<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to access the Django request inside a Template Tag</title>
	<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/</link>
	<description></description>
	<pubDate>Wed, 03 Dec 2008 07:08:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: Zack</title>
		<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-25739</link>
		<author>Zack</author>
		<pubDate>Fri, 18 Jul 2008 17:45:10 +0000</pubDate>
		<guid>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-25739</guid>
		<description>Thanks for the post, many people talk about using context['request'], but not many tell you about needing
TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.request',)
to get it to work.

I was using the context to directly get the GET request like: context['urlinfo'], but it didn't work after sending to a flatpage template, but thanks to you its works like a charm, and I can do this context['request'].GET['urlinfo'].

Thanks!</description>
		<content:encoded><![CDATA[<p>Thanks for the post, many people talk about using context[&#8217;request&#8217;], but not many tell you about needing<br />
TEMPLATE_CONTEXT_PROCESSORS = (&#8217;django.core.context_processors.request&#8217;,)<br />
to get it to work.</p>
<p>I was using the context to directly get the GET request like: context[&#8217;urlinfo&#8217;], but it didn&#8217;t work after sending to a flatpage template, but thanks to you its works like a charm, and I can do this context[&#8217;request&#8217;].GET[&#8217;urlinfo&#8217;].</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-24908</link>
		<author>Nick</author>
		<pubDate>Sun, 22 Jun 2008 21:41:09 +0000</pubDate>
		<guid>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-24908</guid>
		<description>Thanks for the post.

I've found that it's very convinient to use a decorator writing simple line of code like @render_to('mytemplate.html')  instead of passing RequestContext every time like context_instance=RequestContext(request) etc

Here is render_to decorator code. I've found it on internet, so I don't know who is the author.
How to use it:
@render_to('mytemplate.html')
def myview(request):
  return ({'parameter1: 1, 'parameter2':2})

Code:

from django.shortcuts import render_to_response
from django.template import RequestContext

def render_to(template):
    """
    Decorator for Django views that sends returned dict to render_to_response function
    with given template and RequestContext as context instance.

    If view doesn't return dict then decorator simply returns output.
    Additionally view can return two-tuple, which must contain dict as first
    element and string with template name as second. This string will
    override template name, given as parameter

    Parameters:

     - template: template name to use
    """
    def renderer(func):
        def wrapper(request, *args, **kw):
            output = func(request, *args, **kw)
            if isinstance(output, (list, tuple)):
                return render_to_response(output[1], output[0], RequestContext(request))
            elif isinstance(output, dict):
                return render_to_response(template, output, RequestContext(request))
            return output
        return wrapper
    return renderer</description>
		<content:encoded><![CDATA[<p>Thanks for the post.</p>
<p>I&#8217;ve found that it&#8217;s very convinient to use a decorator writing simple line of code like @render_to(&#8217;mytemplate.html&#8217;)  instead of passing RequestContext every time like context_instance=RequestContext(request) etc</p>
<p>Here is render_to decorator code. I&#8217;ve found it on internet, so I don&#8217;t know who is the author.<br />
How to use it:<br />
@render_to(&#8217;mytemplate.html&#8217;)<br />
def myview(request):<br />
  return ({&#8217;parameter1: 1, &#8216;parameter2&#8242;:2})</p>
<p>Code:</p>
<p>from django.shortcuts import render_to_response<br />
from django.template import RequestContext</p>
<p>def render_to(template):<br />
    &#8220;&#8221;"<br />
    Decorator for Django views that sends returned dict to render_to_response function<br />
    with given template and RequestContext as context instance.</p>
<p>    If view doesn&#8217;t return dict then decorator simply returns output.<br />
    Additionally view can return two-tuple, which must contain dict as first<br />
    element and string with template name as second. This string will<br />
    override template name, given as parameter</p>
<p>    Parameters:</p>
<p>     - template: template name to use<br />
    &#8220;&#8221;"<br />
    def renderer(func):<br />
        def wrapper(request, *args, **kw):<br />
            output = func(request, *args, **kw)<br />
            if isinstance(output, (list, tuple)):<br />
                return render_to_response(output[1], output[0], RequestContext(request))<br />
            elif isinstance(output, dict):<br />
                return render_to_response(template, output, RequestContext(request))<br />
            return output<br />
        return wrapper<br />
    return renderer</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rachel</title>
		<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-24877</link>
		<author>Rachel</author>
		<pubDate>Sat, 21 Jun 2008 10:11:56 +0000</pubDate>
		<guid>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-24877</guid>
		<description>Thanks for your comments Daniel - I've updated my post.

(My original post was correct in the course of action to take, but poor in its language to explain it)</description>
		<content:encoded><![CDATA[<p>Thanks for your comments Daniel - I&#8217;ve updated my post.</p>
<p>(My original post was correct in the course of action to take, but poor in its language to explain it)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Roseman</title>
		<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-24876</link>
		<author>Daniel Roseman</author>
		<pubDate>Sat, 21 Jun 2008 10:04:10 +0000</pubDate>
		<guid>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-24876</guid>
		<description>Sorry, this is misleading. You are confusing RequestContext with a particular context processor.

The context processor settings do not determine whether not the context is a RequestContext. This is determined by your view. If you're using a generic view, then it will always be a RequestContext. But if you're using render_to_response, you have to explicitly pass context_instance=RequestContext(request) as the third parameter.

If you do this, your context will be a RequestContext, even if you don't add the Request context processor to your settings. Of course, if you don't do that, you won't have access to the 'request' in your template.</description>
		<content:encoded><![CDATA[<p>Sorry, this is misleading. You are confusing RequestContext with a particular context processor.</p>
<p>The context processor settings do not determine whether not the context is a RequestContext. This is determined by your view. If you&#8217;re using a generic view, then it will always be a RequestContext. But if you&#8217;re using render_to_response, you have to explicitly pass context_instance=RequestContext(request) as the third parameter.</p>
<p>If you do this, your context will be a RequestContext, even if you don&#8217;t add the Request context processor to your settings. Of course, if you don&#8217;t do that, you won&#8217;t have access to the &#8216;request&#8217; in your template.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
