整理复杂的 URL 调度程序

发布于 2024-08-05 04:14:13 字数 639 浏览 5 评论 0原文

我有两种不同类型的对象,我希望它们位于同一 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魄砕の薆 2024-08-12 04:14:13

url 映射通常是明确表达的,但并非必须如此。如何从列表中构建 URL 映射?

foo_urls = ['a', 'b', 'c'] #...
bar_urls = ['x', 'y', 'z'] #...

# A first pattern to get urlpatterns started.
urlpatterns = pattern('project.views', 
    ('blah', 'blah')
    )

# Append all the foo urls.
for foo_url in foo_urls:
    urlpatterns += patterns('project.views',
        ('^' + foo_url + '/
, 'foo')
        )

# Append all the bar urls.
for bar_url in bar_urls:
    urlpatterns += patterns('project.views',
        ('^' + bar_url + '/
, 'bar')
        )

The url maps are usually expressed explicitly, but they don't have to be. How about building your URL map from your lists?

foo_urls = ['a', 'b', 'c'] #...
bar_urls = ['x', 'y', 'z'] #...

# A first pattern to get urlpatterns started.
urlpatterns = pattern('project.views', 
    ('blah', 'blah')
    )

# Append all the foo urls.
for foo_url in foo_urls:
    urlpatterns += patterns('project.views',
        ('^' + foo_url + '/
, 'foo')
        )

# Append all the bar urls.
for bar_url in bar_urls:
    urlpatterns += patterns('project.views',
        ('^' + bar_url + '/
, 'bar')
        )
那一片橙海, 2024-08-12 04:14:13

Django 的 url 模式是正则表达式,因此这确实是可能的:

urlpatterns = patterns('project.views',
    (r'^[abc]/

如果您使用 a、b、c 作为非单字符的示例占位符,则可以使用完整字符串,但要小心确保它们遵循正则表达式匹配规则:

urlpatterns = patterns('project.views',
    (r'^(foo|slithy|toves)/
, 'foo'),
    (r'^[xyz]/

如果您使用 a、b、c 作为非单字符的示例占位符,则可以使用完整字符串,但要小心确保它们遵循正则表达式匹配规则:


, 'bar'),
)

如果您使用 a、b、c 作为非单字符的示例占位符,则可以使用完整字符串,但要小心确保它们遵循正则表达式匹配规则:


, 'foo'),
    (r'^(bar|twas|brillig)/
, 'foo'),
    (r'^[xyz]/

如果您使用 a、b、c 作为非单字符的示例占位符,则可以使用完整字符串,但要小心确保它们遵循正则表达式匹配规则:


, 'bar'),
)

如果您使用 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:

urlpatterns = patterns('project.views',
    (r'^[abc]/

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 = patterns('project.views',
    (r'^(foo|slithy|toves)/
, '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:


, 'foo'),
    (r'^(bar|twas|brillig)/
, '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:

, '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:

心意如水 2024-08-12 04:14:13

您可以将 urlpatterns 替换为捕获所有 url 的模式,然后将在 foobar url 之间进行选择的逻辑移至视图。

urlpatterns = patterns('project.views',
    (r'^(?P<letter>[a-z])/

然后在 views.py 中编写一个函数 foobar

def foobar(request, letter):

    foo_urls = ['a', 'b', 'c'] #...
    bar_urls = ['x', 'y', 'z'] #...
    if slug in foo_urls:
        return foo(request)
    if slug in bar_urls:
        return bar(request)
    else:
         #oh dear, you've caught a
         #url that isn't foo or bar
         #return 404?

作为替代方案,您可能想探索 django 重定向应用,重新设计 url 结构,然后为旧 url 设置重定向。

, 'foobar'), )

然后在 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 and bar urls to the view.

urlpatterns = patterns('project.views',
    (r'^(?P<letter>[a-z])/

Then write a function foobar in views.py

def foobar(request, letter):

    foo_urls = ['a', 'b', 'c'] #...
    bar_urls = ['x', 'y', 'z'] #...
    if slug in foo_urls:
        return foo(request)
    if slug in bar_urls:
        return bar(request)
    else:
         #oh dear, you've caught a
         #url that isn't foo or bar
         #return 404?

As an alternative, you might want to explore the django redirects app, redesign the url structure, then set up redirects for the old urls.

, 'foobar'), )

Then write a function foobar in views.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.

此岸叶落 2024-08-12 04:14:13

如果您的应用程序前面有 Apache:

<LocationMatch "^[a-w]/$">
   ...
</LocationMatch>

<LocationMatch "^[x-z]/$">
   ...
</LocationMatch>

... 可以是您需要的任何配置指令,包括 SetEnv(如果您想要一个环境变量来告诉您是否转到 foo 与 bar,或者可能是 ProxyPass 来发送)对几个不同后端服务器 URL 的请求。

您还可以使用 url 重写或许多其他接受正则表达式参数的 Apache 配置设置。

If you've got Apache in front of your app:

<LocationMatch "^[a-w]/$">
   ...
</LocationMatch>

<LocationMatch "^[x-z]/$">
   ...
</LocationMatch>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文