引发 404 并继续 URL 链
我有一个像这样的 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-list
和 title
匹配相同的内容。
如果存在与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这当然是视图逻辑;所有 urls.py 用于匹配 URL 模式,而不是执行验证。您可以使用
Http404
处理这个异常。或者,您可以找到
get_object_or_404
或 < code>get_list_or_404 方法,这会稍微缩短它。承诺的编辑如下。不完全是您正在寻找的东西,但是...
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.Alternatively, you may find the
get_object_or_404
orget_list_or_404
methods, which shorten it up a bit.Promised edit follows. Not exactly what you're looking for, but...