Ticket #9658: resolve_docs.2.diff

File resolve_docs.2.diff, 1.6 KB (added by mrts, 16 years ago)

Minor correction in example: add request to kwargs

  • docs/topics/http/urls.txt

     
    633633    be imported correctly. Do not include lines that reference views you
    634634    haven't written yet, because those views will not be importable.
    635635
     636resolve()
     637---------
    636638
     639The :func:`django.core.urlresolvers.resolve` function can be used for resolving
     640URL paths to the corresponding view functions. It has the following signature:
     641
     642.. currentmodule:: django.core.urlresolvers
     643.. function:: resolve(path, urlconf=None)
     644
     645``path`` is the URL path you want to resolve. As with ``reverse()`` above, you
     646don't need to worry about the ``urlconf`` parameter. The function returns the
     647triple (view function, arguments, keyword arguments).
     648
     649For example, it can be used for testing if a view would raise a ``Http404``
     650error before redirecting to it::
     651
     652    from urlparse import urlparse
     653    from django.core.urlresolvers import resolve
     654    from django.http import HttpResponseRedirect, Http404
     655
     656    def myview(request):
     657        next = request.META.get('HTTP_REFERER', None) or '/'
     658        response = HttpResponseRedirect(next)
     659
     660        # modify the request and response as required, e.g. change locale
     661        # and set corresponding locale cookie
     662
     663        view, args, kwargs = resolve(urlparse(next)[2])
     664        kwargs['request'] = request
     665        try:
     666            view(*args, **kwargs)
     667        except Http404:
     668            return HttpResponseRedirect('/')
     669        return response
     670
    637671permalink()
    638672-----------
    639673
Back to Top