<?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>Lior Gradstein'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, 13 May 2010 09:46:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 difference [...]]]></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="python" style="font-family: monospace;color: #000066; border: 1px solid orange; margin: 5px; padding: 5px; background-color: #ffffff;">url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&#8216;foo/ajouter/$&#8217;</span>, <span style="color: #483d8b;">&#8216;django.views.generic.create_update.create_object&#8217;</span>, &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>form_class=modelForm,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extra_context=<span style="color: black;">&#123;</span><span style="color: #483d8b;">&#8216;usage&#8217;</span>:<span style="color: #483d8b;">&#8216;create&#8217;</span><span style="color: black;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name=<span style="color: #483d8b;">&#8216;foo_create&#8217;</span>,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></p>
<p>url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&#8216;foo/%s/(?P&lt;object_id&gt;<span style="color: #000099; font-weight: bold;">\d</span>+)/modifier/$&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&#8216;django.views.generic.create_update.update_object&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>form_class=modelForm,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extra_context=<span style="color: black;">&#123;</span><span style="color: #483d8b;">&#8216;usage&#8217;</span>:<span style="color: #483d8b;">&#8216;modify&#8217;</span><span style="color: black;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name=<span style="color: #483d8b;">&#8216;foo_update&#8217;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></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="python" style="font-family: monospace;color: #000066; border: 1px solid orange; margin: 5px; padding: 5px; background-color: #ffffff;"><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</div>
<p>and in the url(), add context_processors:</p>
<div class="python" style="font-family: monospace;color: #000066; border: 1px solid orange; margin: 5px; padding: 5px; background-color: #ffffff;">url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&#8216;foo/ajouter/$&#8217;</span>, <span style="color: #483d8b;">&#8216;django.views.generic.create_update.create_object&#8217;</span>, &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>form_class=modelForm,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context_processors=<span style="color: black;">&#91;</span>request,<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name=<span style="color: #483d8b;">&#8216;foo_create&#8217;</span>,<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></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="python" style="font-family: monospace;color: #000066; border: 1px solid orange; margin: 5px; padding: 5px; background-color: #ffffff;"><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;django.core.context_processors.auth&quot;</span>,<br />
<span style="color: #483d8b;">&quot;django.core.context_processors.debug&quot;</span>,<br />
<span style="color: #483d8b;">&quot;django.core.context_processors.i18n&quot;</span>,<br />
<span style="color: #483d8b;">&quot;django.core.context_processors.media&quot;</span><span style="color: black;">&#41;</span></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>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li>No related posts.</li>
	</ul>

]]></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>
