#536 closed defect (fixed)
Wrap load_and_render in HttpResponse in the calling function
| Reported by: | Boffbowsh | Owned by: | Adrian Holovaty |
|---|---|---|---|
| Component: | Tools | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | paul.bowsher@… | Triage Stage: | Design decision needed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Using this syntax would be better as not everyone needs to return an HttpResponse:
return HttpResponse(load_and_render('index', 'title': 'Page Title', 'primes': [2, 3, 5, 7], 'header': 'The first 4 primes:'))
Patch:
Index: /usr/local/django_src/django/core/extensions.py
===================================================================
--- /usr/local/django_src/django/core/extensions.py (revision 661)
+++ /usr/local/django_src/django/core/extensions.py (working copy)
@@ -3,7 +3,6 @@
from django.core import template_loader
from django.core.template import Context
from django.conf.settings import DEBUG, INTERNAL_IPS
-from django.utils.httpwrappers import HttpResponse
def load_and_render(template_name, dictionary=None, context_instance=None):
dictionary = dictionary or {}
@@ -12,7 +11,7 @@
context_instance.update(dictionary)
else:
context_instance = Context(dictionary)
- return HttpResponse(t.render(context_instance))
+ return t.render(context_instance)
class DjangoContext(Context):
"""
Change History (6)
comment:1 by , 20 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
comment:2 by , 20 years ago
| Resolution: | wontfix |
|---|---|
| Status: | closed → reopened |
How about this then?
Index: /usr/local/django_src/django/core/extensions.py
===================================================================
--- /usr/local/django_src/django/core/extensions.py (revision 661)
+++ /usr/local/django_src/django/core/extensions.py (working copy)
@@ -5,15 +5,18 @@
from django.conf.settings import DEBUG, INTERNAL_IPS
from django.utils.httpwrappers import HttpResponse
-def load_and_render(template_name, dictionary=None, context_instance=None):
+def load_and_return(template_name, dictionary=None, context_instance=None):
dictionary = dictionary or {}
t = template_loader.get_template(template_name)
if context_instance:
context_instance.update(dictionary)
else:
context_instance = Context(dictionary)
- return HttpResponse(t.render(context_instance))
+ return t.render(context_instance)
+def load_and_render(*args, **kwargs):
+ return HttpResponse(load_and_return(*args, **kwargs))
+
class DjangoContext(Context):
"""
This subclass of template.Context automatically populates 'user' and
Gives a load_and_return function, which is used in turn by load_and_render
comment:3 by , 20 years ago
| Cc: | added |
|---|
comment:4 by , 20 years ago
That's fine by me, although load_and_return() is easily confused with load_and_render(). What's a better name?
comment:5 by , 20 years ago
render_to_response and render_to_string? I know people have already started to use load_and_render, but i think it's a good idea to catch it now. The current name is too ambiguous anyway imo.
Note:
See TracTickets
for help on using tickets.
load_and_render()is a shortcut for the most common case: returning anHttpResponseof the rendered template. Removing theHttpResponsewould defeat much of the purpose of this shortcut.