This document is for Django's development version, which can be significantly different from previo
Django shortcut functions¶
The package django.shortcuts collects helper functions and classes that “span” multiple levels of MVC. In other words, these functions/classes introduce controlled coupling for convenience’s sake.
render_to_response¶
render_to_response(template[, dictionary][, context_instance][, mimetype])¶
Renders a given template with a given context dictionary and returns an HttpResponse object with that rendered text.
Required arguments¶
template
The full name of a template to use or sequence of template names.
Optional arguments¶
dictionary
A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.
context_instance
The context instance to render the template with. By default, the template will be rendered with a Context instance (filled with values from dictionary). If you need to use context processors, render the template with a RequestContext instance instead. Your code might look something like this:
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
mimetype
New in Django 1.0: Please, see the release notes
The MIME type to use for the resulting document. Defaults to the value of the DEFAULT_CONTENT_TYPE setting.
Example¶
The following example renders the template myapp/index.html with the MIME type application/xhtml+xml:
from django.shortcuts import render_to_response
def my_view(request):
# View code here...
return render_to_response('myapp/index.html', {"foo": "bar"},
mimetype="application/xhtml+xml")
This example is equivalent to:
from django.http import HttpResponse
from django.template import Context, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/template.html')
c = Context({'foo': 'bar'})
return HttpResponse(t.render(c),
mimetype="application/xhtml+xml")
redirect¶
redirect(to[, permanent=False], *args, **kwargs)¶
New in Django 1.1: Please, see the release notes
Returns an HttpResponseRedirect to the apropriate URL for the arguments passed.
The arguments could be:
* A model: the model's get_absolute_url() function will be called.
* A view name, possibly with arguments: urlresolvers.reverse() will be used to reverse-resolve the name.
* A URL, which will be used as-is for the redirect location.
By default issues a temporary redirect; pass permanent=True to issue a permanent redirect
Examples¶
You can use the redirect() function in a number of ways.
1.
By passing some object; that object's get_absolute_url() method will be called to figure out the redirect URL:
The package django.shortcuts collects helper functions and classes that “span” multiple levels of MVC. In other words, these functions/classes introduce controlled coupling for convenience’s sake.
render_to_response¶
render_to_response(template[, dictionary][, context_instance][, mimetype])¶
Renders a given template with a given context dictionary and returns an HttpResponse object with that rendered text.
Required arguments¶
template
The full name of a template to use or sequence of template names.
Optional arguments¶
dictionary
A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.
context_instance
The context instance to render the template with. By default, the template will be rendered with a Context instance (filled with values from dictionary). If you need to use context processors, render the template with a RequestContext instance instead. Your code might look something like this:
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
mimetype
New in Django 1.0: Please, see the release notes
The MIME type to use for the resulting document. Defaults to the value of the DEFAULT_CONTENT_TYPE setting.
Example¶
The following example renders the template myapp/index.html with the MIME type application/xhtml+xml:
from django.shortcuts import render_to_response
def my_view(request):
# View code here...
return render_to_response('myapp/index.html', {"foo": "bar"},
mimetype="application/xhtml+xml")
This example is equivalent to:
from django.http import HttpResponse
from django.template import Context, loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/template.html')
c = Context({'foo': 'bar'})
return HttpResponse(t.render(c),
mimetype="application/xhtml+xml")
redirect¶
redirect(to[, permanent=False], *args, **kwargs)¶
New in Django 1.1: Please, see the release notes
Returns an HttpResponseRedirect to the apropriate URL for the arguments passed.
The arguments could be:
* A model: the model's get_absolute_url() function will be called.
* A view name, possibly with arguments: urlresolvers.reverse() will be used to reverse-resolve the name.
* A URL, which will be used as-is for the redirect location.
By default issues a temporary redirect; pass permanent=True to issue a permanent redirect
Examples¶
You can use the redirect() function in a number of ways.
1.
By passing some object; that object's get_absolute_url() method will be called to figure out the redirect URL: