resolve()的一个可能用途是测试 视图是否会在重定向到它之前引发Http404错误:
app/views.py:
from urlparse import urlparse
from django.core.urlresolvers import resolve
from django.http import HttpResponseRedirect, Http404
def myview(request):
next = request.META.get('HTTP_REFERER', None) or '/'
response = HttpResponseRedirect(next)
# modify the request and response as required, e.g. change locale
# and set corresponding locale cookie
view, args, kwargs = resolve(urlparse(next)[2])
kwargs['request'] = request
try:
view(*args, **kwargs)
except Http404:
return HttpResponseRedirect('/')
return response
如何使用这是我的问题? 请考虑以下情况:
Invalid URL -- http://example.com/givemesomething
Valid URL -- http://example.com/giveone
我的urlConf
将
urlpatterns=("",
url(r'^$', 'blog.index'),
url(r'^giveone/$', 'blog.getone'),
#redirect everything else to blog.index
)
我必须完成所有请求views.myview
(上面),验证然后重定向。那个urlpattern
会是什么?