Django

Code

Changeset 6234

Show
Ignore:
Timestamp:
09/14/07 17:33:06 (1 year ago)
Author:
adrian
Message:

Fixed #5474 -- Improved docs/shortcuts.txt. Thanks, minarets and EyePulp?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/shortcuts.txt

    r6215 r6234  
    77introduce controlled coupling for convenience's sake. 
    88 
    9 ``render_to_response`` 
    10 ====================== 
     9``render_to_response()`` 
     10======================== 
    1111 
    1212``django.shortcuts.render_to_response`` renders a given template with a given 
     
    1414text. 
    1515 
    16 Example:: 
     16Required arguments 
     17------------------ 
     18 
     19``template`` 
     20    The full name of a template to use. 
     21 
     22Optional arguments 
     23------------------ 
     24 
     25``context`` 
     26    A dictionary of values to add to the template context. By default, this 
     27    is an empty dictionary. If a value in the dictionary is callable, the 
     28    view will call it just before rendering  the template. 
     29 
     30``mimetype`` 
     31    **New in Django development version:** The MIME type to use for the 
     32    resulting document. Defaults to the value of the ``DEFAULT_CONTENT_TYPE`` 
     33    setting. 
     34 
     35Example 
     36------- 
     37 
     38The following example renders the template ``myapp/index.html`` with the 
     39MIME type ``application/xhtml+xml``:: 
    1740 
    1841    from django.shortcuts import render_to_response 
    19     r = render_to_response('myapp/template.html', {'foo': 'bar'}) 
     42 
     43    def my_view(request): 
     44        # View code here... 
     45        return render_to_response('myapp/index.html', {"foo": "bar"}, 
     46            mimetype="application/xhtml+xml") 
    2047 
    2148This example is equivalent to:: 
     
    2350    from django.http import HttpResponse 
    2451    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)) 
     52 
     53    def my_view(request): 
     54        # View code here... 
     55        t = loader.get_template('myapp/template.html') 
     56        c = Context({'foo': 'bar'}) 
     57        r = HttpResponse(t.render(c), 
     58            mimetype="application/xhtml+xml") 
     59 
     60.. _an HttpResponse object: ../request_response/#httpresponse-objects 
    2861 
    2962``get_object_or_404`` 
    3063===================== 
    3164 
    32 ``django.shortcuts.get_object_or_404`` calls ``get()`` on a given model 
     65``django.shortcuts.get_object_or_404`` calls `get()`_ on a given model 
    3366manager, but it raises ``django.http.Http404`` instead of the model's 
    3467``DoesNotExist`` exception. 
     68 
     69Required arguments 
     70------------------ 
     71 
     72``klass`` 
     73    A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the 
     74    object. 
     75 
     76``**kwargs`` 
     77    Lookup parameters, which should be in the format accepted by ``get()`` and 
     78    ``filter()``. 
     79 
     80Example 
     81------- 
     82 
     83The following example gets the object with the primary key of 1 from 
     84``MyModel``:: 
     85 
     86    from django.shortcuts import get_object_or_404 
     87 
     88    def my_view(request): 
     89        my_object = get_object_or_404(MyModel, pk=1) 
     90 
     91This example is equivalent to:: 
     92 
     93    from django.http import Http404 
     94 
     95    def my_view(request): 
     96        try: 
     97            my_object = MyModel.object.get(pk=1) 
     98        except MyModel.DoesNotExist: 
     99            raise Http404 
     100 
     101Note: As with ``get()``, an ``AssertionError`` will be raised if more than 
     102one object is found. 
     103 
     104.. _get(): ../db-api/#get-kwargs 
    35105 
    36106``get_list_or_404`` 
    37107=================== 
    38108 
    39 ``django.shortcuts.get_list_or_404`` returns the result of ``filter()`` on a 
     109``django.shortcuts.get_list_or_404`` returns the result of `filter()`_ on a 
    40110given model manager, raising ``django.http.Http404`` if the resulting list is 
    41111empty. 
     112 
     113Required arguments 
     114------------------ 
     115 
     116``klass`` 
     117    A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the 
     118    object. 
     119 
     120``**kwargs`` 
     121    Lookup parameters, which should be in the format accepted by ``get()`` and 
     122    ``filter()``. 
     123 
     124Example 
     125------- 
     126 
     127The following example gets all published objects from ``MyModel``:: 
     128 
     129    from django.shortcuts import get_list_or_404 
     130 
     131    def my_view(request): 
     132        my_objects = get_list_or_404(MyModel, published=True) 
     133 
     134This example is equivalent to:: 
     135 
     136    from django.http import Http404 
     137 
     138    def my_view(request): 
     139        my_objects = MyModels.object.filter(published=True) 
     140        if not my_objects: 
     141            raise Http404 
     142 
     143.. _filter(): ../db-api/#filter-kwargs