我想在我的应用程序的URL中有一个可选的slug,比如Stack Overflows如何处理它的URL:
http://stackoverflow.com/questions/<question_id>/<question_slug>
,
其中<question_slug>
是可选的。也就是说,如果你去
http://stackoverflow.com/questions/<question_id>
,您将被重定向到
http://stackoverflow.com/questions/<question_id>/<question_slug>
我的urls.py:
url(r'^myapp/(?P<thing_id>\d+)/edit_thing/$', views.edit_thing, name='edit-thing'),
url(r'^myapp/(?P<thing_id>\d+)/(?P<thing_slug>[\w-]+)/edit_thing/$', views.edit_thing, name='edit-thing2'),
我的views.py:
def edit_thing(request, thing_id, thing_slug=None):
thing = get_object_or_404(Thing, pk=thing_id)
if thing_slug is None:
thing_slug = thing.slug
HttpResponseRedirect(reverse('myapp:edit_thing2', kwargs={'thing_id':thing_id, 'thing_slug':thing_slug}))
# ... continued ...
这似乎有效,因为myapp/1/
会渲染我想要显示的模板,但浏览器中的URL不会像我想要的那样更新到myapp/1/<model-1-slug>
。我错过了什么?我不能像这样重定向到同一个视图吗?
答案 0 :(得分:1)