Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#536 closed defect (fixed)

Wrap load_and_render in HttpResponse in the calling function

Reported by: Boffbowsh Owned by: adrian
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: UI/UX:

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 Changed 10 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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 Changed 10 years ago by Boffbowsh

  • Resolution wontfix deleted
  • Status changed from closed to 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 Changed 10 years ago by anonymous

  • Cc paul.bowsher@… added

comment:4 Changed 10 years ago by adrian

That's fine by me, although load_and_return() is easily confused with load_and_render(). What's a better name?

comment:5 Changed 10 years ago by Boffbowsh

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 Changed 10 years ago by adrian

  • Resolution set to fixed
  • Status changed from reopened to closed

Done in [664].

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