引发 404 并继续 URL 链

发布于 2024-08-02 17:54:06 字数 601 浏览 4 评论 0原文

我有一个像这样的 URL 模式:

urlpatterns = (
    url(r'^$', list_titles, name='list'),
    url(r'^(?P<tag>[a-z\-0-9]+?)/$', list_titles, name='filtered-list'),
    url(r'^(?P<title>\S+?)/$', show_title, name='title'),
)

filtered-listtitle 匹配相同的内容。

如果存在与 filtered-list 中的 tag 匹配的可用内容列表,我希望触发 list_titles。但如果没有匹配的 tag,我想将其冒泡回 URL 处理器,以便 show_title 触发。

如果没有匹配的标题,我会在那里提出适当的 404。

我知道我可以从视图内部执行此操作...但是必须将过程硬连接到视图中有点难闻。我希望 URL 顺序决定首先选择什么以及将其交给什么。

I've got a URLs pattern like this:

urlpatterns = (
    url(r'^

The filtered-list and title match the same things.

If there is an available list of things matching the tag in filtered-list, I want list_titles to fire off. But if there isn't a matching tag, I want to bubble that back to the URL processor so show_title fires off.

If there's no matching title, I'll raise a proper 404 there.

I know I can do this from inside the view...but it's a bit smelly having to hard-wire the process into the view. I'd like the URL order to decide what gets chosen first and what it hands off to.

, list_titles, name='list'), url(r'^(?P<tag>[a-z\-0-9]+?)/

The filtered-list and title match the same things.

If there is an available list of things matching the tag in filtered-list, I want list_titles to fire off. But if there isn't a matching tag, I want to bubble that back to the URL processor so show_title fires off.

If there's no matching title, I'll raise a proper 404 there.

I know I can do this from inside the view...but it's a bit smelly having to hard-wire the process into the view. I'd like the URL order to decide what gets chosen first and what it hands off to.

, list_titles, name='filtered-list'), url(r'^(?P<title>\S+?)/

The filtered-list and title match the same things.

If there is an available list of things matching the tag in filtered-list, I want list_titles to fire off. But if there isn't a matching tag, I want to bubble that back to the URL processor so show_title fires off.

If there's no matching title, I'll raise a proper 404 there.

I know I can do this from inside the view...but it's a bit smelly having to hard-wire the process into the view. I'd like the URL order to decide what gets chosen first and what it hands off to.

, show_title, name='title'), )

The filtered-list and title match the same things.

If there is an available list of things matching the tag in filtered-list, I want list_titles to fire off. But if there isn't a matching tag, I want to bubble that back to the URL processor so show_title fires off.

If there's no matching title, I'll raise a proper 404 there.

I know I can do this from inside the view...but it's a bit smelly having to hard-wire the process into the view. I'd like the URL order to decide what gets chosen first and what it hands off to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一杆小烟枪 2024-08-09 17:54:06

这当然是视图逻辑;所有 urls.py 用于匹配 URL 模式,而不是执行验证。您可以使用 Http404 处理这个异常。

from django.http import Http404

def detail(request, poll_id):
    try:
        p = Poll.objects.get(pk=poll_id)
    except Poll.DoesNotExist:
        raise Http404
    return render_to_response('polls/detail.html', {'poll': p})

或者,您可以找到 get_object_or_404< code>get_list_or_404 方法,这会稍微缩短它。


承诺的编辑如下。不完全是您正在寻找的东西,但是...

urlpatterns = (
    url(r'^
, list_titles, name='list'),
)

if 1=1: # Your logic here
    urlpatterns += ( url(r'^
, list_titles, name='list'), )

urlpatterns += (
    url(r'^(?P<title>\S+?)/
, show_title, name='title'),
    url(r'^spam/
, spam_bar),
    url(r'^foo/
, foo_bar),
}

This is certainly view logic; all urls.py is for is for matching URL patterns, not performing validation. You can use the Http404 exception to handle this.

from django.http import Http404

def detail(request, poll_id):
    try:
        p = Poll.objects.get(pk=poll_id)
    except Poll.DoesNotExist:
        raise Http404
    return render_to_response('polls/detail.html', {'poll': p})

Alternatively, you may find the get_object_or_404 or get_list_or_404 methods, which shorten it up a bit.


Promised edit follows. Not exactly what you're looking for, but...

urlpatterns = (
    url(r'^
, list_titles, name='list'),
)

if 1=1: # Your logic here
    urlpatterns += ( url(r'^
, list_titles, name='list'), )

urlpatterns += (
    url(r'^(?P<title>\S+?)/
, show_title, name='title'),
    url(r'^spam/
, spam_bar),
    url(r'^foo/
, foo_bar),
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文