Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added django.views.generic.simple.redirect_to view for issuing simple…
… redirects. Also updated direct_to_template to use render_to_response to be consistant with coding style, and documented the simple generic views. git-svn-id: http://code.djangoproject.com/svn/django/trunk@1249 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
Showing
2 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
from django.core import template_loader | ||
from django.core.extensions import DjangoContext | ||
from django.utils.httpwrappers import HttpResponse | ||
from django.core.extensions import DjangoContext, render_to_response | ||
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect, HttpResponseGone | ||
|
||
def direct_to_template(request, template, **kwargs): | ||
"""Render a given template with any extra parameters in the context.""" | ||
t = template_loader.get_template(template) | ||
c = DjangoContext(request, {'params' : kwargs}) | ||
return HttpResponse(t.render(c)) | ||
""" | ||
Render a given template with any extra URL parameters in the context as | ||
``{{ params }}``. | ||
""" | ||
return render_to_response(template, {'params' : kwargs}, context_instance=DjangoContext(request)) | ||
|
||
def redirect_to(request, url, **kwargs): | ||
""" | ||
Redirect to a given URL. | ||
The given url may contain dict-style string formatting which will be | ||
interpolated against the params in the URL. For example, to redirect from | ||
``/foo/<id>/`` to ``/bar/<id>/``, you could use the following urlpattern:: | ||
urlpatterns = patterns('', | ||
('^foo/(?p<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}), | ||
) | ||
If the given url is ``None``, a HttpResponseGone (410) will be issued. | ||
""" | ||
if url is not None: | ||
return HttpResponseRedirect(url % kwargs) | ||
else: | ||
return HttpResponseGone() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters