如何使用resolve(url)重定向Django的主页?

时间:2014-03-19 19:44:34

标签: django django-views django-urls http-redirect

我在Django documentation:

中看到了这个例子
  

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会是什么?

0 个答案:

没有答案