Ticket #5474: 5475.diff

File 5475.diff, 4.5 KB (added by Maura Chace, 17 years ago)

shortcuts docs

  • shortcuts.txt

     
    66"span" multiple levels of MVC. In other words, these functions/classes
    77introduce controlled coupling for convenience's sake.
    88
    9 ``render_to_response``
    10 ======================
     9The ``render_to_response()`` shortcut
     10=====================================
    1111
    12 ``django.shortcuts.render_to_response`` renders a given template with a given
    13 context dictionary and returns an ``HttpResponse`` object with that rendered
    14 text.
     12Django provides a shortcut, ``django.shortcuts.render_to_response``, for loading a template, filling
     13its context, and returning `an HttpResponse object`_.
    1514
    16 Example::
     15**Required arguments:**
    1716
     17    ``template``
     18        The full name of a template to use.
     19
     20**Optional arguments:**
     21
     22    ``context``
     23        A dictionary of values to add to the template
     24        context. By default, this is an empty dictionary. If a value in the
     25        dictionary is callable, the view will call it just before rendering
     26        the template.
     27
     28    ``mimetype``
     29        The MIME type to use for the resulting document. Defaults
     30        to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
     31
     32**Example:**
     33
     34The following example renders the template ``myapp/index.html`` with the mimetype ``application/xhtml+xml``::
     35
    1836    from django.shortcuts import render_to_response
    19     r = render_to_response('myapp/template.html', {'foo': 'bar'})
     37   
     38    def my_view(request):
     39        # View code here
     40        return render_to_response("myapp/index.html", {"foo": "bar"}, mimetype="application/xhtml+xml")
    2041
    2142This example is equivalent to::
    2243
    2344    from django.http import HttpResponse
    2445    from django.template import Context, loader
    25     t = loader.get_template('myapp/template.html')
    26     c = Context({'foo': 'bar'})
    27     r = HttpResponse(t.render(c))
     46   
     47    def my_view(request):
     48        # View code here
     49        t = loader.get_template('myapp/template.html')
     50        c = Context({'foo': 'bar'})
     51        r = HttpResponse(t.render(c), mimetype="application/xhtml+xml")
     52   
     53.. _an HttpResponse object: ../request_response/#httpresponse-objects
    2854
    29 ``get_object_or_404``
    30 =====================
     55The ``get_object_or_404`` shortcut
     56==================================
    3157
    32 ``django.shortcuts.get_object_or_404`` calls ``get()`` on a given model
     58``django.shortcuts.get_object_or_404`` calls `get()`_ on a given model
    3359manager, but it raises ``django.http.Http404`` instead of the model's
    3460``DoesNotExist`` exception.
    3561
    36 ``get_list_or_404``
    37 ===================
     62**Required arguments:**
     63    klass
     64        A Model, Manager, or Queryset object from which to get the object.
     65       
     66    kwargs
     67        Lookup parameters, which should be in the format described in
     68        Field lookups.
     69       
     70The following example gets the object with the primary key of 1 from ``MyModel``::
    3871
    39 ``django.shortcuts.get_list_or_404`` returns the result of ``filter()`` on a
     72    from django.shortcuts import get_object_or_404
     73   
     74    def my_view(request):
     75        my_object = get_object_or_404(MyModel, pk=1)
     76       
     77This example is equivalent to::
     78
     79    from django.core.exceptions import ObjectDoesNotExist
     80    from django.http import Http404
     81   
     82    def my_view(request):
     83        try:
     84            my_object = MyModels.object.get(pk=1)
     85        except ObjectDoesNotExist:
     86            raise Http404
     87
     88Note: Like with get(), an AssertionError will be raised if more than one
     89object is found.
     90
     91.. _get(): ../db-api/#get-kwargs
     92
     93The ``get_list_or_404`` shortcut
     94================================
     95
     96``django.shortcuts.get_list_or_404`` returns the result of `filter()`_ on a
    4097given model manager, raising ``django.http.Http404`` if the resulting list is
    4198empty.
     99
     100**Required arguments:**
     101    klass
     102        A Model, Manager, or Queryset object from which to get the object.
     103       
     104    kwargs
     105        Lookup parameters, which should be in the format described in
     106        Field lookups.
     107       
     108The following example gets all published objects from ``MyModel``::
     109
     110    from django.shortcuts import get_list_or_404
     111
     112    def my_view(request):
     113        my_objects = get_list_or_404(MyModel, published=True)
     114
     115This example is equivalent to::
     116
     117    from django.http import Http404
     118
     119    def my_view(request):
     120        my_objects = MyModels.object.filter(published=True)
     121        if not my_objects:
     122            raise Http404
     123
     124.. _filter(): ../db-api/#filter-kwargs
Back to Top