Opened 19 years ago

Closed 19 years ago

Last modified 17 years ago

#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 Adrian Holovaty, 19 years ago

Resolution: wontfix
Status: newclosed

load_and_render() is a shortcut for the most common case: returning an HttpResponse of the rendered template. Removing the HttpResponse would defeat much of the purpose of this shortcut.

comment:2 by Boffbowsh, 19 years ago

Resolution: wontfix
Status: closedreopened

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 anonymous, 19 years ago

Cc: paul.bowsher@… added

comment:4 by Adrian Holovaty, 19 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 Boffbowsh, 19 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.

comment:6 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: reopenedclosed

Done in [664].

Note: See TracTickets for help on using tickets.
Back to Top