<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Luc Stepniewski&#039;s Blog &#187; django</title>
	<atom:link href="http://www.gradstein.info/category/software/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gradstein.info</link>
	<description></description>
	<lastBuildDate>Thu, 29 Dec 2011 22:59:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<atom:link rel='hub' href='http://www.gradstein.info/?pushpress=hub'/>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Django: How to find the url/path you&#8217;re into, in a template loaded by a generic view</title>
		<link>http://www.gradstein.info/python/django-how-to-find-the-urlpath-youre-into-in-a-template-loaded-by-a-generic-view/</link>
		<comments>http://www.gradstein.info/python/django-how-to-find-the-urlpath-youre-into-in-a-template-loaded-by-a-generic-view/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 22:11:39 +0000</pubDate>
		<dc:creator>Lior Gradstein</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.gradstein.info/?p=197</guid>
		<description><![CDATA[In a Django project, I have a template that is used by two urls, which is quite common (generic views, using &#8216;create_object&#8217; and &#8216;update_object&#8217;). The problem is that I had to add a supplementary menu just when the template is loaded from the &#8216;update&#8217; generic view, and not from the &#8216;create&#8217; generic view. Making the [...]]]></description>
			<content:encoded><![CDATA[<p>In a Django project, I have a template that is used by two urls, which is quite common (generic views, using &#8216;create_object&#8217; and &#8216;update_object&#8217;). The problem is that I had to add a supplementary menu just when the template is loaded from the &#8216;update&#8217; generic view, and not from the &#8216;create&#8217; generic view.</p>
<p>Making the difference between the two urls calls at the template level is a problem because it&#8217;s managed by generic views, so the same template is used.</p>
<p>Anyways, there are several possibilities:</p>
<p>In urls.py, use the <a href="http://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-create-update-create-object" class="liexternal">&#8216;template_name&#8217; variable</a>, where you can speficy a specific template for this url(). That is instead of using the default &lt;model&gt;_form.html.<br />
What I don&#8217;t like in this situation, is that I will have two nearly similar templates, just for an added menu. Not cool. Another problem is that I use <a href="http://code.djangoproject.com/wiki/GenerateGenericURLs" class="liexternal">a loop to create all my urls</a>. So if I add a special template, I&#8217;ll add it to <strong>ALL my models</strong> :-(.</p>
<p>Another solution, is to find a way to use a variable in the template that would be different wether the template has been loaded by update_object or create_object.</p>
<p>In our urlpatterns in urls.py, we can use the &#8216;<strong>extra_context</strong>&#8216; variable (takes a <a href="http://docs.djangoproject.com/en/dev/ref/generic-views/?from=olddocs#django-views-generic-create-update-create-object" class="liexternal">dictionnary as parameter</a>). It is correctly managed, even when using generic views. So, you&#8217;ll have :</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'foo/ajouter/$'</span>, <span style="color: #483d8b;">'django.views.generic.create_update.create_object'</span>,  
		<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>form_class=modelForm,
                extra_context=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'usage'</span>:<span style="color: #483d8b;">'create'</span><span style="color: black;">&#125;</span>,
                name=<span style="color: #483d8b;">'foo_create'</span>,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'foo/%s/(?P&lt;object_id&gt;<span style="color: #000099; font-weight: bold;">\d</span>+)/modifier/$'</span>,
                <span style="color: #483d8b;">'django.views.generic.create_update.update_object'</span>,
		<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>form_class=modelForm,
                extra_context=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'usage'</span>:<span style="color: #483d8b;">'modify'</span><span style="color: black;">&#125;</span>,
                name=<span style="color: #483d8b;">'foo_update'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>We can also use, in urls.py, the <a href="http://docs.djangoproject.com/en/dev/ref/generic-views/?from=olddocs#django-views-generic-create-update-create-object" class="liexternal">&#8216;context_processors&#8217; variable</a>. For more information about the context processors, have <a href="http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/" class="liexternal">a look at this tutorial</a>. The goal is to add &#8216;django.core.context_processors.request&#8217;, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">context_processors</span> <span style="color: #ff7700;font-weight:bold;">import</span> request</pre></div></div>

<p>and in the url(), add context_processors:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'foo/ajouter/$'</span>, <span style="color: #483d8b;">'django.views.generic.create_update.create_object'</span>,  
		<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>form_class=modelForm,
		context_processors=<span style="color: black;">&#91;</span>request,<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>,
                name=<span style="color: #483d8b;">'foo_create'</span>,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The last possiblity is a more global solution. It&#8217;s like the context_processors usage above, but added into every templates automatically.<br />
To do this, you&#8217;ll have to edit the list of Template Processors in your settings.py file. That list is run each time a template is loaded, and allows one to add any variable to the template automatically. By default (on Django 1.0.x) this list is commented out, so it has by <a href="http://docs.djangoproject.com/en/dev/ref/settings/#setting-TEMPLATE_CONTEXT_PROCESSORS" class="liexternal">default the list</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;django.core.context_processors.auth&quot;</span>,
<span style="color: #483d8b;">&quot;django.core.context_processors.debug&quot;</span>,
<span style="color: #483d8b;">&quot;django.core.context_processors.i18n&quot;</span>,
<span style="color: #483d8b;">&quot;django.core.context_processors.media&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>You&#8217;ll have to uncomment it, and add &#8216;django.core.context_processors.request&#8217;. By doing this, you get the variable &#8216;request.path&#8217; available in your template.</p>
<p>Finally, you&#8217;ll be able to test your variable with {% ifequal %} and display your conditional elements.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gradstein.info/python/django-how-to-find-the-urlpath-youre-into-in-a-template-loaded-by-a-generic-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using memcached

Served from: www.stepniewski.fr @ 2012-02-06 07:55:53 -->
