<?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>Code-It &#187; django</title>
	<atom:link href="http://lois.di-qual.net/blog/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://lois.di-qual.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 30 Oct 2012 19:19:29 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>django-wkhtmltopdf on a linux (Webfaction) server</title>
		<link>http://lois.di-qual.net/blog/django-wkhtmltopdf-on-a-linux-webfaction-server/</link>
		<comments>http://lois.di-qual.net/blog/django-wkhtmltopdf-on-a-linux-webfaction-server/#comments</comments>
		<pubDate>Tue, 04 Sep 2012 13:10:08 +0000</pubDate>
		<dc:creator>Loïs Di Qual</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[webfaction]]></category>

		<guid isPermaLink="false">http://lois.di-qual.net/blog/?p=119</guid>
		<description><![CDATA[Generating PDF with python is not an easy thing. Here is what I&#8217;ve tried: xhtml2pdf: the worst. It has a very bad CSS interpreter and it is pretty impossible to make a clean PDF out from a complex html page. WeasyPrint: that&#8217;s better. It has its own HTML and CSS2 interpreters, and despite some minor [...]]]></description>
				<content:encoded><![CDATA[<p>Generating PDF with python is not an easy thing. Here is what I&#8217;ve tried:</p>
<ul>
<li><a href="http://www.xhtml2pdf.com/">xhtml2pdf</a>: the worst. It has a very bad CSS interpreter and it is pretty impossible to make a clean PDF out from a complex html page.</li>
<li><a href="http://weasyprint.org/">WeasyPrint</a>: that&#8217;s better. It has its own HTML and CSS2 interpreters, and despite some minor issues it is fairly reliable. However it requires <a href="http://lois.di-qual.net/blog/python-install-weasyprint-on-a-dedicated-webfaction-server/">some big dependencies</a>, and I eventually had some memory issues on my webserver.</li>
<li><a href="https://code.google.com/p/wkhtmltopdf/">wkhtmltopdf</a> with its <a href="https://github.com/incuna/django-wkhtmltopdf">Django wrapper</a>: for now, the best. It makes use of Webkit to render HTML+CSS+JS(!) pages, I don&#8217;t think you can do better. However, it requires QT, and this is big. Well, not the &#8220;classical&#8221; QT, a patched QT (that&#8217;s even worse) that allows to run without an X server. This is what I&#8217;m using for now in my projects</li>
</ul>
<p>We&#8217;ll see here how to build wkhtmltopdf and a basic <code class="codecolorer text default"><span class="text">django-wkhtmltopdf</span></code> usage. These steps has been tested on a Webfaction server (CentOS 6) through SSH.</p>
<p><span id="more-119"></span></p>
<h2>The easy way</h2>
<p>Enter this command and go to the &#8220;Post-install&#8221; chapter.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">curl -fsSkL https://raw.github.com/gist/3608048/wkhtmltopdf.sh | sh</div></td></tr></tbody></table></div>
<p>It will install wkhtmltopdf in <code class="codecolorer text default"><span class="text">~/bin</span></code> with its static libraries. </p>
<h2>The hard way &#8211; Let&#8217;s build this</h2>
<p>This is mostly taken from the <a href="https://code.google.com/p/wkhtmltopdf/wiki/compilation">wkhtmltopdf Wiki</a>, but with some no-shared-libraries webserver specifics. Log on SSH, and let the magic begin.</p>
<p>Create a <code class="codecolorer text default"><span class="text">~/download</span></code> folder</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ mkdir -p ~/downloads<br />
$ cd ~/downloads</div></td></tr></tbody></table></div>
<p>Get wkhtmltopdf-qt:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git clone git://gitorious.org/+wkhtml2pdf/qt/wkhtmltopdf-qt.git wkhtmltopdf-qt<br />
$ cd wkhtmltopdf-qt<br />
$ git checkout staging<br />
$ QTDIR=. ./bin/syncqt</div></td></tr></tbody></table></div>
<p>Configure, and build it! It will take some time (about an hour, which is not much as we disabled a lot of features):</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ ./configure -release -static -fast -exceptions -no-accessibility -no-stl -no-sql-ibase -no-sql-mysql -no-sql-odbc -no-sql-psql -no-sql-sqlite -no-sql-sqlite2 -no-qt3support -xmlpatterns -no-phonon -no-phonon-backend -webkit -no-scripttools -no-mmx -no-3dnow -no-sse -no-sse2 &nbsp;-qt-zlib -qt-libtiff -qt-libpng -qt-libmng -qt-libjpeg -graphicssystem raster -opensource -nomake tools -nomake examples -nomake demos -nomake docs -nomake translations -no-opengl -no-dbus -no-multimedia -openssl -no-declarative -largefile -rpath -no-nis -no-cups -no-iconv -no-pch -no-gtkstyle -no-nas-sound -no-sm -no-xshape -no-xinerama -no-xcursor -no-xfixes -no-xrandr -no-mitshm -no-xinput -no-xkb -no-glib -no-openvg &nbsp;-no-xsync -no-audio-backend -no-sse3 -no-ssse3 -no-sse4.1 -no-sse4.2 -no-avx -no-neon -confirm-license -prefix &quot;../wkqt&quot;<br />
$ gmake -j3<br />
$ gmake install</div></td></tr></tbody></table></div>
<p>Then, download wkhtmltopdf, and build it:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cd ~/downloads<br />
$ git clone git://github.com/antialize/wkhtmltopdf.git wkhtmltopdf<br />
$ cd wkhtmltopdf<br />
$ ../wkqt/bin/qmake<br />
$ make</div></td></tr></tbody></table></div>
<p>You now have wkhtmltopdf and its static dependencies in <code class="codecolorer text default"><span class="text">./bin/</span></code>. Let&#8217;s copy them into <code class="codecolorer text default"><span class="text">~/bin</span></code>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ mkdir -p ~/bin<br />
$ cp bin/* ~/bin/</div></td></tr></tbody></table></div>
<h2>Post-installation</h2>
<p>We need <code class="codecolorer text default"><span class="text">~/bin</span></code> in our <code class="codecolorer text default"><span class="text">$PATH</span></code>. That&#8217;s where django-wkhtmltopdf will look for. Add the following line in your <code class="codecolorer text default"><span class="text">~/.bashrc</span></code>.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">export PATH=$HOME/bin:$PATH<br />
export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH</div></td></tr></tbody></table></div>
<p>And source it:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ source ~/.bashrc</div></td></tr></tbody></table></div>
<p>We&#8217;re done with wkhtmltopdf, let&#8217;s install its Django wrapper.</p>
<h2>django-wkhtmltopdf</h2>
<p>Go to your django app, and install django-wkhtmltopdf with PIP:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ pip install wkhtmltopdf</div></td></tr></tbody></table></div>
<p>Then, in your view:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> wkhtmltopdf.<span style="color: black;">views</span> <span style="color: #ff7700;font-weight:bold;">import</span> PDFTemplateResponse<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> view<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; PDFTemplateResponse<span style="color: black;">&#40;</span>request<span style="color: #66cc66;">,</span> template_name<span style="color: #66cc66;">,</span> context_dictionary<span style="color: black;">&#41;</span>.<span style="color: black;">rendered_content</span></div></td></tr></tbody></table></div>
<p>Be careful, this last line of code is not documented in the django-wkhtmltopdf&#8217;s wiki. It may be subject to changes in future releases. However, that&#8217;s the only way I found to get a binary-string instead of a HttpResponse.</p>
<p>That&#8217;s all, folks !</p>
]]></content:encoded>
			<wfw:commentRss>http://lois.di-qual.net/blog/django-wkhtmltopdf-on-a-linux-webfaction-server/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Django &#8211; UnicodeDecodeError using i18n</title>
		<link>http://lois.di-qual.net/blog/django-unicodedecodeerror-using-i18n/</link>
		<comments>http://lois.di-qual.net/blog/django-unicodedecodeerror-using-i18n/#comments</comments>
		<pubDate>Mon, 30 Jul 2012 21:32:11 +0000</pubDate>
		<dc:creator>Loïs Di Qual</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[UnicodeDecodeError]]></category>

		<guid isPermaLink="false">http://lois.di-qual.net/blog/?p=83</guid>
		<description><![CDATA[If you&#8217;re using i18n in your django website, you may encounter this issue. It took me two hours to find where the problem came from: you have to use ugettext instead of gettext if your translated strings contains non-ascii characters. gettext => ugettext With gettext: 12345from django.utils.translation import gettext as _ define my_view&#40;request&#41;: &#160; &#160; [...]]]></description>
				<content:encoded><![CDATA[<p>If you&#8217;re using i18n in your django website, you may encounter this issue. It took me two hours to find where the problem came from: you have to use <code class="codecolorer text default"><span class="text">ugettext</span></code> instead of <code class="codecolorer text default"><span class="text">gettext</span></code> if your translated strings contains non-ascii characters.</p>
<p><span id="more-83"></span></p>
<h2>gettext => ugettext</h2>
<p>With <code class="codecolorer text default"><span class="text">gettext</span></code>:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">utils</span>.<span style="color: black;">translation</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">gettext</span> <span style="color: #ff7700;font-weight:bold;">as</span> _<br />
<br />
define my_view<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><span style="color: black;">&#41;</span> // Prints <span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes something spécial&quot;</span><span style="color: black;">&#41;</span> // Raises a <span style="color: #008000;">UnicodeDecodeError</span> because <span style="color: #483d8b;">&quot;é&quot;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">unicode</span></div></td></tr></tbody></table></div>
<p>With <code class="codecolorer text default"><span class="text">ugettext</span></code>:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">utils</span>.<span style="color: black;">translation</span> <span style="color: #ff7700;font-weight:bold;">import</span> ugettext <span style="color: #ff7700;font-weight:bold;">as</span> _<br />
<br />
define my_view<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><span style="color: black;">&#41;</span> // Prints <span style="color: #483d8b;">&quot;Here comes nothing&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> _<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Here comes something spécial&quot;</span><span style="color: black;">&#41;</span> // Prints <span style="color: #483d8b;">&quot;Here comes something spécial&quot;</span></div></td></tr></tbody></table></div>
<h2>ugettext or ugettext_lazy ?</h2>
<p>Two simple rules:</p>
<ul>
<li>Use <code class="codecolorer text default"><span class="text">ugettext_lazy</span></code> in global scopes (model fields, django settings, everything not wrapped by a function or method, &#8230;)</li>
<li>Use <code class="codecolorer text default"><span class="text">ugettext</span></code> everywhere else (in views, methods, functions, &#8230;)</li>
</ul>
<p>That&#8217;s it !</p>
]]></content:encoded>
			<wfw:commentRss>http://lois.di-qual.net/blog/django-unicodedecodeerror-using-i18n/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django &#8211; Absolute media URIs from templates</title>
		<link>http://lois.di-qual.net/blog/django-absolute-media-uris-from-templates/</link>
		<comments>http://lois.di-qual.net/blog/django-absolute-media-uris-from-templates/#comments</comments>
		<pubDate>Mon, 16 Jul 2012 21:54:17 +0000</pubDate>
		<dc:creator>Loïs Di Qual</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://lois.di-qual.net/blog/?p=74</guid>
		<description><![CDATA[Sometimes, you need to build an absolute URI to a media file from your templates: 1http://example.com/media/sample.jpg After following this small tutorial, you&#8217;ll be able to do this: 12{% load mytemplatetags %} {{ media_name&#124;absolute_media_url:request }} Settings In urls.py, in urlpatterns, add a reverse name to the media URL: 123456urlpatterns = patterns&#40;'', &#160; &#160; # Before &#160; [...]]]></description>
				<content:encoded><![CDATA[<p>Sometimes, you need to build an absolute URI to a media file from your templates:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">http://example.com/media/sample.jpg</div></td></tr></tbody></table></div>
<p>After following this small tutorial, you&#8217;ll be able to do this:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{% load mytemplatetags %}<br />
{{ media_name|absolute_media_url:request }}</div></td></tr></tbody></table></div>
<p><span id="more-74"></span></p>
<h2>Settings</h2>
<p>In <code class="codecolorer text default"><span class="text">urls.py</span></code>, in <code class="codecolorer text default"><span class="text">urlpatterns</span></code>, add a reverse name to the media URL:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">urlpatterns <span style="color: #66cc66;">=</span> patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Before</span><br />
&nbsp; &nbsp; <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^media/(?P&lt;path&gt;.*)$'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'django.views.static.serve'</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'document_root'</span>: settings.<span style="color: black;">MEDIA_ROOT</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># After</span><br />
&nbsp; &nbsp; url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^media/(?P&lt;path&gt;.*)$'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'django.views.static.serve'</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'document_root'</span>: settings.<span style="color: black;">MEDIA_ROOT</span><span style="color: black;">&#125;</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">=</span><span style="color: #483d8b;">&quot;media&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>You can now reverse media files using <code class="codecolorer text default"><span class="text">request.build_absolute_uri(reverse('media', args=['/path/to/media']))</span></code>.</p>
<p>In <code class="codecolorer text default"><span class="text">settings.py</span></code>, add <code class="codecolorer text default"><span class="text">django.core.context_processors.request</span></code> to <code class="codecolorer text default"><span class="text">TEMPLATE_CONTEXT_PROCESSORS</span></code>.</p>
<p>This context processor allows you to access the <code class="codecolorer text default"><span class="text">request.build_absolute_uri()</span></code> function from templates:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> request.<span style="color: black;">build_absolute_uri</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span></div></td></tr></tbody></table></div>
<p>However, you can&#8217;t pass parameters to this function, so you won&#8217;t be able to reverse media path to URL. That&#8217;s why we need a filter.</p>
<h2>Absolute url for a media</h2>
<p>The following filter will allow you to build an absolute URL to a media. In <code class="codecolorer text default"><span class="text">templatetags/mytemplatetags.py</span></code>:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #483d8b;">'''<br />
Usage: {{ media|absolute_media_url:request }}<br />
'''</span><br />
<span style="color: #66cc66;">@</span>register.<span style="color: #008000;">filter</span><br />
<span style="color: #ff7700;font-weight:bold;">def</span> absolute_media_uri<span style="color: black;">&#40;</span>media<span style="color: #66cc66;">,</span> request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> request.<span style="color: black;">build_absolute_uri</span><span style="color: black;">&#40;</span>reverse<span style="color: black;">&#40;</span><span style="color: #483d8b;">'media'</span><span style="color: #66cc66;">,</span> args<span style="color: #66cc66;">=</span><span style="color: black;">&#91;</span><span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>media<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>Don&#8217;t forget to add a blank <code class="codecolorer text default"><span class="text">__init__.py</span></code> in <code class="codecolorer text default"><span class="text">templatetags/</span></code> or django won&#8217;t take your template tag into account.</p>
]]></content:encoded>
			<wfw:commentRss>http://lois.di-qual.net/blog/django-absolute-media-uris-from-templates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
