<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to access the Django request inside a Template Tag</title>
	<atom:link href="http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/</link>
	<description></description>
	<lastBuildDate>Thu, 09 Feb 2012 20:48:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: jnns</title>
		<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/comment-page-1/#comment-47011</link>
		<dc:creator>jnns</dc:creator>
		<pubDate>Mon, 04 Oct 2010 13:24:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/#comment-47011</guid>
		<description>Thanks for the heads up on context[&quot;request&quot;]!</description>
		<content:encoded><![CDATA[<p>Thanks for the heads up on context["request"]!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zack</title>
		<link>http://www.willmer.com/kb/2008/06/how-to-access-the-django-request-inside-a-template-tag/comment-page-1/#comment-25739</link>
		<dc:creator>Zack</dc:creator>
		<pubDate>Fri, 18 Jul 2008 17:45:10 +0000</pubDate>
		<guid isPermaLink="false">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[&#039;request&#039;], but not many tell you about needing
TEMPLATE_CONTEXT_PROCESSORS = (&#039;django.core.context_processors.request&#039;,)
to get it to work.

I was using the context to directly get the GET request like: context[&#039;urlinfo&#039;], but it didn&#039;t work after sending to a flatpage template, but thanks to you its works like a charm, and I can do this context[&#039;request&#039;].GET[&#039;urlinfo&#039;].

Thanks!</description>
		<content:encoded><![CDATA[<p>Thanks for the post, many people talk about using context['request'], but not many tell you about needing<br />
TEMPLATE_CONTEXT_PROCESSORS = (&#8216;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['urlinfo'], 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['request'].GET['urlinfo'].</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-page-1/#comment-24908</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Sun, 22 Jun 2008 21:41:09 +0000</pubDate>
		<guid isPermaLink="false">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&#039;ve found that it&#039;s very convinient to use a decorator writing simple line of code like @render_to(&#039;mytemplate.html&#039;)  instead of passing RequestContext every time like context_instance=RequestContext(request) etc

Here is render_to decorator code. I&#039;ve found it on internet, so I don&#039;t know who is the author.
How to use it:
@render_to(&#039;mytemplate.html&#039;)
def myview(request):
  return ({&#039;parameter1: 1, &#039;parameter2&#039;:2})

Code:

from django.shortcuts import render_to_response
from django.template import RequestContext

def render_to(template):
    &quot;&quot;&quot;
    Decorator for Django views that sends returned dict to render_to_response function
    with given template and RequestContext as context instance.

    If view doesn&#039;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
    &quot;&quot;&quot;
    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(&#8216;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(&#8216;mytemplate.html&#8217;)<br />
def myview(request):<br />
  return ({&#8216;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>     &#8211; 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-page-1/#comment-24877</link>
		<dc:creator>Rachel</dc:creator>
		<pubDate>Sat, 21 Jun 2008 10:11:56 +0000</pubDate>
		<guid isPermaLink="false">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&#039;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 &#8211; 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-page-1/#comment-24876</link>
		<dc:creator>Daniel Roseman</dc:creator>
		<pubDate>Sat, 21 Jun 2008 10:04:10 +0000</pubDate>
		<guid isPermaLink="false">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&#039;re using a generic view, then it will always be a RequestContext. But if you&#039;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&#039;t add the Request context processor to your settings. Of course, if you don&#039;t do that, you won&#039;t have access to the &#039;request&#039; 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>

