Ticket #10194: redirect2.diff
File redirect2.diff, 1.8 KB (added by , 16 years ago) |
---|
-
django/shortcuts/__init__.py
5 5 """ 6 6 7 7 from django.template import loader 8 from django.http import HttpResponse, Http 4048 from django.http import HttpResponse, HttpResponseRedirect, Http404 9 9 from django.db.models.manager import Manager 10 10 from django.db.models.query import QuerySet 11 11 … … 60 60 if not obj_list: 61 61 raise Http404('No %s matches the given query.' % queryset.model._meta.object_name) 62 62 return obj_list 63 64 def redirect( obj ): 65 """ 66 Returns a HttpResponseRedirect, calling get_absolute_url() on the passed 67 argument if it exists. 68 """ 69 redirect_to = hasattr(obj, 'get_absolute_url') and obj.get_absolute_url() or obj 70 return HttpResponseRedirect( redirect_to ) -
docs/topics/http/shortcuts.txt
151 151 my_objects = list(MyModel.objects.filter(published=True)) 152 152 if not my_objects: 153 153 raise Http404 154 155 ``redirect`` 156 ============ 157 158 .. versionadded:: 1.1 159 160 ``django.shortcuts.redirect`` returns an ``HttpResponseRedirect`` object with 161 the given string or model instance. 162 163 Required arguments 164 ------------------ 165 166 ``redirect_to`` 167 Either a string, or any object with a ``get_absolute_url()`` method defined 168 for it. 169 170 Example 171 ------- 172 173 The following example redirects to a newly-created model instance:: 174 175 from django.shortcuts import redirect 176 177 def my_view(request): 178 if request.method == 'POST': 179 # Create a model instance here... 180 return redirect(model_inst)