mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #12816 -- Added a render() shortcut.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15008 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d4ef841495
commit
b3d2091681
8 changed files with 195 additions and 14 deletions
|
@ -12,6 +12,70 @@ 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``
|
||||
==========
|
||||
|
||||
.. function:: render(request, template[, dictionary][, context_instance][, mimetype])
|
||||
|
||||
Combines a given template with a given context dictionary and returns an
|
||||
:class:`~django.http.HttpResponse` object with that rendered text.
|
||||
|
||||
:func:`render()` is the same as a call to
|
||||
:func:`render_to_response()` with a context_instance argument that
|
||||
that forces the use of a :class:`RequestContext`.
|
||||
|
||||
Required arguments
|
||||
------------------
|
||||
|
||||
``request``
|
||||
The request object used to generate this response.
|
||||
|
||||
``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 ``RequestContext`` instance (filled with values from
|
||||
``request`` and ```dictionary``).
|
||||
|
||||
``mimetype``
|
||||
The MIME type to use for the resulting document. Defaults to the value of
|
||||
the :setting:`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 = RequestContext(request, {'foo': 'bar'})
|
||||
return HttpResponse(t.render(c),
|
||||
mimetype="application/xhtml+xml")
|
||||
|
||||
|
||||
``render_to_response``
|
||||
======================
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue