整理复杂的 URL 调度程序
我有两种不同类型的对象,我希望它们位于同一 URL 下。一组对象需要传递给视图函数“foo”,另一组需要传递给“bar”。
我目前正在使用一长串硬编码 URL 来执行此操作,就像这样...
urlpatterns = patterns('project.views',
(r'^a/$', 'foo'),
(r'^b/$', 'foo'),
(r'^c/$', 'foo'),
#...and so on until...
(r'^x/$', 'bar'),
(r'^y/$', 'bar'),
(r'^z/$', 'bar'),
)
是否可以定义每种类型的 URL 的列表,例如...
foo_urls = ['a', 'b', 'c'] #...
bar_urls = ['x', 'y', 'z'] #...
...然后根据这些列表检查传入的 URL? (如果它在'foo_urls'中,则发送到'project.views.foo';如果它在'bar_urls'中,则发送到'project.views.bar')?
我仅限于保留此结构以保持与前一个站点的 URL 的兼容性,但任何有关简化 urls.py 方法的建议将不胜感激。
I have two different kinds of objects that I'd like to live under the same URL. One group of objects needs to be passed to the view function 'foo' and another group needs to be passed to 'bar'.
I'm currently doing this with a big long list of hardcoded URLs, like so...
urlpatterns = patterns('project.views',
(r'^a/
Is it possible to define a list of each type of URLs like...
foo_urls = ['a', 'b', 'c'] #...
bar_urls = ['x', 'y', 'z'] #...
...and then check the incoming URL against those lists? (If it's in 'foo_urls', send to 'project.views.foo'; if it's in 'bar_urls', send to 'project.views.bar')?
I'm limited to keeping this structure to maintain compatibility with the URLs from the previous site, but any advice on ways to simplify my urls.py would be much appreciated.
, 'foo'),
(r'^b/
Is it possible to define a list of each type of URLs like...
...and then check the incoming URL against those lists? (If it's in 'foo_urls', send to 'project.views.foo'; if it's in 'bar_urls', send to 'project.views.bar')?
I'm limited to keeping this structure to maintain compatibility with the URLs from the previous site, but any advice on ways to simplify my urls.py would be much appreciated.
, 'foo'),
(r'^c/
Is it possible to define a list of each type of URLs like...
...and then check the incoming URL against those lists? (If it's in 'foo_urls', send to 'project.views.foo'; if it's in 'bar_urls', send to 'project.views.bar')?
I'm limited to keeping this structure to maintain compatibility with the URLs from the previous site, but any advice on ways to simplify my urls.py would be much appreciated.
, 'foo'),
#...and so on until...
(r'^x/
Is it possible to define a list of each type of URLs like...
...and then check the incoming URL against those lists? (If it's in 'foo_urls', send to 'project.views.foo'; if it's in 'bar_urls', send to 'project.views.bar')?
I'm limited to keeping this structure to maintain compatibility with the URLs from the previous site, but any advice on ways to simplify my urls.py would be much appreciated.
, 'bar'),
(r'^y/
Is it possible to define a list of each type of URLs like...
...and then check the incoming URL against those lists? (If it's in 'foo_urls', send to 'project.views.foo'; if it's in 'bar_urls', send to 'project.views.bar')?
I'm limited to keeping this structure to maintain compatibility with the URLs from the previous site, but any advice on ways to simplify my urls.py would be much appreciated.
, 'bar'),
(r'^z/
Is it possible to define a list of each type of URLs like...
...and then check the incoming URL against those lists? (If it's in 'foo_urls', send to 'project.views.foo'; if it's in 'bar_urls', send to 'project.views.bar')?
I'm limited to keeping this structure to maintain compatibility with the URLs from the previous site, but any advice on ways to simplify my urls.py would be much appreciated.
, 'bar'),
)
Is it possible to define a list of each type of URLs like...
...and then check the incoming URL against those lists? (If it's in 'foo_urls', send to 'project.views.foo'; if it's in 'bar_urls', send to 'project.views.bar')?
I'm limited to keeping this structure to maintain compatibility with the URLs from the previous site, but any advice on ways to simplify my urls.py would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
url 映射通常是明确表达的,但并非必须如此。如何从列表中构建 URL 映射?
The url maps are usually expressed explicitly, but they don't have to be. How about building your URL map from your lists?
Django 的 url 模式是正则表达式,因此这确实是可能的:
如果您使用 a、b、c 作为非单字符的示例占位符,则可以使用完整字符串,但要小心确保它们遵循正则表达式匹配规则:
, 'bar'), ) , 'foo'), (r'^[xyz]/如果您使用 a、b、c 作为非单字符的示例占位符,则可以使用完整字符串,但要小心确保它们遵循正则表达式匹配规则:
, 'bar'), )如果您使用 a、b、c 作为非单字符的示例占位符,则可以使用完整字符串,但要小心确保它们遵循正则表达式匹配规则:
Django's url patterns are regular expressions, so this is indeed possible:
If you're using a, b, c as example placeholders for a non-single-character, you can use the full strings instead, but be careful to make sure they follow regex matching rules:
, 'bar'), ) , 'foo'), (r'^[xyz]/If you're using a, b, c as example placeholders for a non-single-character, you can use the full strings instead, but be careful to make sure they follow regex matching rules:
, 'bar'), )If you're using a, b, c as example placeholders for a non-single-character, you can use the full strings instead, but be careful to make sure they follow regex matching rules:
您可以将 urlpatterns 替换为捕获所有 url 的模式,然后将在
foo
和bar
url 之间进行选择的逻辑移至视图。然后在
views.py
中编写一个函数foobar
作为替代方案,您可能想探索 django 重定向应用,重新设计 url 结构,然后为旧 url 设置重定向。
You could replace the urlpatterns with one that catches all the urls, then move the logic to choose between
foo
andbar
urls to the view.Then write a function
foobar
inviews.py
As an alternative, you might want to explore the django redirects app, redesign the url structure, then set up redirects for the old urls.
如果您的应用程序前面有 Apache:
... 可以是您需要的任何配置指令,包括 SetEnv(如果您想要一个环境变量来告诉您是否转到 foo 与 bar,或者可能是 ProxyPass 来发送)对几个不同后端服务器 URL 的请求。
您还可以使用 url 重写或许多其他接受正则表达式参数的 Apache 配置设置。
If you've got Apache in front of your app:
The ... can be any config directives you need, including SetEnv if you'd like an environment variable to tell you whether to go to foo vs bar, or maybe ProxyPass to send the request to a couple of different backend server urls.
You can also use url rewriting or a number of other Apache config settings that accept regex arguments.