Ticket #5474: 5475.diff
File 5475.diff, 4.5 KB (added by , 17 years ago) |
---|
-
shortcuts.txt
6 6 "span" multiple levels of MVC. In other words, these functions/classes 7 7 introduce controlled coupling for convenience's sake. 8 8 9 ``render_to_response`` 10 ====================== 9 The ``render_to_response()`` shortcut 10 ===================================== 11 11 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. 12 Django provides a shortcut, ``django.shortcuts.render_to_response``, for loading a template, filling 13 its context, and returning `an HttpResponse object`_. 15 14 16 Example:: 15 **Required arguments:** 17 16 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 34 The following example renders the template ``myapp/index.html`` with the mimetype ``application/xhtml+xml``:: 35 18 36 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") 20 41 21 42 This example is equivalent to:: 22 43 23 44 from django.http import HttpResponse 24 45 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 28 54 29 ``get_object_or_404`` 30 ===================== 55 The ``get_object_or_404`` shortcut 56 ================================== 31 57 32 ``django.shortcuts.get_object_or_404`` calls ` `get()``on a given model58 ``django.shortcuts.get_object_or_404`` calls `get()`_ on a given model 33 59 manager, but it raises ``django.http.Http404`` instead of the model's 34 60 ``DoesNotExist`` exception. 35 61 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 70 The following example gets the object with the primary key of 1 from ``MyModel``:: 38 71 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 77 This 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 88 Note: Like with get(), an AssertionError will be raised if more than one 89 object is found. 90 91 .. _get(): ../db-api/#get-kwargs 92 93 The ``get_list_or_404`` shortcut 94 ================================ 95 96 ``django.shortcuts.get_list_or_404`` returns the result of `filter()`_ on a 40 97 given model manager, raising ``django.http.Http404`` if the resulting list is 41 98 empty. 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 108 The 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 115 This 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