Ticket #650 - render_to_response uses Context instead DjangoContext

11 views
Skip to first unread message

Paul Bowsher

unread,
Jun 25, 2007, 6:45:21 PM6/25/07
to Django developers
Hi all,

Just coming back to Django after an 18 month absense, great to see
some of the work that's gone on here, i'm really impressed! Been using
Rails for a while (not by choice) and it's a joy to get back into
Django.

Enough of the fluffing though, and down to business.

render_to_response is a great shortcut, however in my opinion it's let
down by not supporting request. I see this has been discussed a bit on
the ticket, however that's more in respect to replacing the existing
shortcut.

I propose a new shortcut of the following form:

Index: django/shortcuts/__init__.py
===================================================================
--- django/shortcuts/__init__.py (revision 5526)
+++ django/shortcuts/__init__.py (working copy)
@@ -5,11 +5,16 @@
from django.template import loader
from django.http import HttpResponse, Http404
from django.db.models.manager import Manager
+from django.template.context import RequestContext

def render_to_response(*args, **kwargs):
return HttpResponse(loader.render_to_string(*args, **kwargs))
load_and_render = render_to_response # For backwards compatibility.

+def render_to_response_with_req(request, *args, **kwargs):
+ kwargs['context_instance'] = RequestContext(request)
+ return render_to_response(*args, **kwargs)
+
def get_object_or_404(klass, *args, **kwargs):
if isinstance(klass, Manager):
manager = klass

def get_object_or_404(klass, *args, **kwargs):
if isinstance(klass, Manager):
manager = klass

If anyone has any ideas for better names, please chip in.

Regards,

Paul

SmileyChris

unread,
Jun 25, 2007, 10:45:54 PM6/25/07
to Django developers
On Jun 26, 10:45 am, Paul Bowsher <paul.bows...@gmail.com> wrote:
> Hi all,

>
> Been using
> Rails for a while (not by choice) and it's a joy to get back into
> Django.

Welcome back ;)

> render_to_response is a great shortcut, however in my opinion it's let
> down by not supporting request.

render_to_response(context_instance=RequestContext(request))
Convoluted, and it means having to import RequestContext, but it is
possible.

I agree, an official shortcut would be good. This comes up at least
daily on IRC, with the usual response being http://www.djangosnippets.org/snippets/3/

Russell Keith-Magee

unread,
Jun 26, 2007, 11:51:34 PM6/26/07
to django-d...@googlegroups.com
On 6/26/07, Paul Bowsher <paul.b...@gmail.com> wrote:
>
> render_to_response is a great shortcut, however in my opinion it's let
> down by not supporting request. I see this has been discussed a bit on
> the ticket, however that's more in respect to replacing the existing
> shortcut.

Isn't this just the 'direct_to_template' generic view?

Yours,
Russ Magee %-)

Paul Bowsher

unread,
Jun 27, 2007, 4:46:56 AM6/27/07
to django-d...@googlegroups.com
No, this is meant to go at the end of a long view function
def index(request):
    # Lots of code
    render_to_response_with_req(request, 'home/index.html', {'a': 'dict'})

etc.

SmileyChris

unread,
Jun 27, 2007, 5:24:37 AM6/27/07
to Django developers
On Jun 27, 8:46 pm, "Paul Bowsher" <paul.bows...@gmail.com> wrote:
> No, this is meant to go at the end of a long view function
> def index(request):
> # Lots of code
> render_to_response_with_req(request, 'home/index.html', {'a': 'dict'})

Firstly, you should be `return`ing this request. ;)

Secondly, it looks like the direct_to_template function does exactly
this (albeit with a tiny bit of overhead in checking for and calling
callable context items).

Paul Bowsher

unread,
Jun 27, 2007, 7:54:03 AM6/27/07
to django-d...@googlegroups.com
Yes, sorry, I missed the return, was hastily typed as an example. Hadn't seen direct_to_template, that does indeed seem like a good way to go. Thanks for the pointer :)
--
Paul Bowsher
Reply all
Reply to author
Forward
0 new messages