Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » add RequestContext automatically to each render_to_response call [RSS Feed]

#1 March 10, 2008 10:28:27

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

add RequestContext automatically to each render_to_response call


hi,

I've been writing a django-application and now I need the
RequestContext for some new features. I don't want to add
context_instance=RequestContext(request) to each render_to_response
call manually in my views.py.

How can I can I solve my problem?

I could do this with retrieving the request-object and do something
like this:

from django.shortcuts import render_to_response as _render_to_response
from django.template import RequestContext
from django.anywher import request

def render_to_response(*args, **kwargs):
kwargs = RequestContext(request)
return _render_to_response(*args, **kwargs)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 10, 2008 15:40:21

Nathaniel W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

add RequestContext automatically to each render_to_response call


On Mar 10, 3:28 am, Julian <> wrote:
> def render_to_response(*args, **kwargs):
> kwargs = RequestContext(request)
> return _render_to_response(*args, **kwargs)

That's exactly how Snippet #3 does it. I personally prefer using
the built-in direct_to_template() .

.. http://www.djangosnippets.org/snippets/3/.. http://groups.google.com/group/django-developers/msg/6df67e5f58e768a3--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 10, 2008 15:40:25

W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

add RequestContext automatically to each render_to_response call


On 10 мар, 11:28, Julian <> wrote:
> hi,
>
> I've been writing a django-application and now I need the
> RequestContext for some new features. I don't want to add
> context_instance=RequestContext(request) to each render_to_response
> call manually in my views.py.
>
> How can I can I solve my problem?
>
> I could do this with retrieving the request-object and do something
> like this:
>
> from django.shortcuts import render_to_response as _render_to_response
> from django.template import RequestContext
> from django.anywher import request
>
> def render_to_response(*args, **kwargs):
> kwargs = RequestContext(request)
> return _render_to_response(*args, **kwargs)

You can use also custom decorator, for example:

from django.shortcuts import render_to_response
from django.template import RequestContext
def render_to(tmpl):
def renderer(func):
def wrapper(request, *args, **kw):
output = func(request, *args, **kw)
if not isinstance(output, dict):
return output
return render_to_response(tmpl, output,
context_instance=RequestContext(request))
return wrapper
return renderer

Usage:

@render_to('path/to/template.html')
def my_cool_view(request):
my_var = 'Cool!'
return {'myvar':myvar}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

  • Root
  • » Django
  • » add RequestContext automatically to each render_to_response call [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support