Django: render, redirect, reverse, resolve

Let’s try to highlight every useful ‘r… thing’ in Django.

Shortcut functions (django.shortcuts)


render(request, template_name, context=None, content_type=None, status=None, using=None)

Combines a template with a context dictionary, returns an HttpResponse object with that rendered text.


Render to response

Deprecated since v2.0!

render_to_response(template_name, context=None, content_type=None, status=None, using=None)

Works similarly to render(), except that it doesn’t make the request available in the response. To use RequestContext you have to specify context_instance=RequestContext(request)



redirect(to, permanent=False, *args, **kwargs)

Returns HttpResponseRedirect object to the appropriate URL for the arguments passed:

  • model: will call get_absolute_url() function
  • view (with optional arguments): will use reverse() function to reverse-resolve the name.
  • URL (absolute or relative):  will be used as-is for the redirect location.


Utility functions (django.urls)


reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)

Returns URL to the view as string. Similar functionality to the url template tag which is used to convert namespaced url to real url pattern.
Viewname can be a URL name or the callable view function/class. If the URL accepts arguments, you may pass them in args OR kwargs.


Reverse lazy

reverse_lazy(viewname, urlconf=None, args=None, kwargs=None, current_app=None)

Returns object, a lazily evaluated version of reverse() when you need to use a URL reversal before your project’s URLConf is loaded for example in:

  • generic class-based view (because in Python class attributes are evaluated on import)
  • decorator (eg. permission_required()).
  • default value for a parameter in a function’s signature.



resolve(path, urlconf=None)

Resolves URL paths to the corresponding view functions.

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Leave a Reply