<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Michele Campeotto: Tag django</title>
    <link>http://blog.micampe.it/articles/tag/django?tag=django</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>I'm not Winston Wolfe.</description>
    <item>
      <title>A little Django feature nobody tells you about</title>
      <description>&lt;p&gt;Recently i got involved in a &lt;a href="http://it.blogbabel.com/"&gt;new project&lt;/a&gt; which uses Django, so I started learning and using it. I won&amp;#8217;t tell you here it merits as those have alread been described at lenght.&lt;/p&gt;

&lt;p&gt;One of the features I like about Django are its pluggable applications: you can build a generic piece of code and that can easily be plugged in other&amp;#8217;s sites to get some new functionality. Great examples of this are found in the &lt;code&gt;django.contrib&lt;/code&gt; package, which include the already famous administrative interface, two comments systems, an authentication/authorization framework and more.&lt;/p&gt;

&lt;p&gt;One problem you&amp;#8217;ll soon find is that some of these are tied to be plugged at a fixed URL prefix. The auth system, for example, expects to be plugged in the &lt;code&gt;/accounts/&lt;/code&gt; directory and plugging it somewhere else leads to some more work for you. The easiest example is the &lt;code&gt;@login_required&lt;/code&gt; decorator, which checks the user that is calling a view and, if it is not authenticated, it redirects to &lt;code&gt;/accounts/login&lt;/code&gt;. If you have used a different URL prefix, you are forced to use the &lt;code&gt;@user_passes_test&lt;/code&gt; decorator, which allows you to set the redirect URL.&lt;/p&gt;

&lt;p&gt;Where is the problem? The problem is that those paths are explicitly hardocded in the views.&lt;/p&gt;

&lt;p&gt;How to solve it? Dynamically build your URLs from your &lt;code&gt;urlconf&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How do you do that? With &lt;code&gt;django.core.urlresolvers.reverse()&lt;/code&gt;&lt;/p&gt;&lt;p&gt;That little boy helps you by giving you the URL path that would be called given a view and a set of parameters. Here is an example, suppose your urlconf has something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;urlpatterns = patterns('',
    (r'^/blog/(?P&amp;lt;year&amp;gt;\d{4})/(?P&amp;lt;month&amp;gt;\d{2})/(?P&amp;lt;day&amp;gt;\d{2})/(?P&amp;lt;slug&amp;gt;[-\w]+)/$',
        'dlog.blog.views.entry'),
)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;you could create a function like this to get the URL of an entry:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    def get_absolute_url(self):
        return urlresolvers.reverse('dlog.blog.views.entry',
                kwargs={'year':self.pubdate.year,
                        'month':('%02d' % self.pubdate.month),
                        'day':('%02d' % self.pubdate.day),
                        'slug':self.slug})
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;when you then call an entry&amp;#8217;s &lt;code&gt;get_absolute_url()&lt;/code&gt; method, you&amp;#8217;ll get the URL as urlconf expects it.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/blog/2007/01/06/your-slug-goes-here
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I don&amp;#8217;t know why this isn&amp;#8217;t mentioned anywhere in the docs, I found it by hunting down the source code because I didn&amp;#8217;t like hardcoding paths in my views.&lt;/p&gt;

&lt;p&gt;I can understand it has some problems: it gives you only URLs relative to the root of the Django project, if you need an absolute URL or if you attached the site to a subdirectory of your domain, &lt;code&gt;urlresolvers.reverse()&lt;/code&gt; is not enough, but I still think it should be advertised in the documentation.&lt;/p&gt;

&lt;p&gt;One more thing, as everybody starting to use Django, I wrote a web log with it. But I haven&amp;#8217;t replaced this one, I created a new one, but this time in Italian and that will be more personal and, hopefully, a real blog that I update it more often than monthly and I actually &lt;a href="http://it.blogbabel.com/"&gt;participate&lt;/a&gt;. So, if you can read Italian follow me on &lt;a href="http://michele.campeotto.net/"&gt;my new corner&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Sat, 06 Jan 2007 07:38:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:f507f9ce-ec95-4420-a60b-845a11c08ef0</guid>
      <author>micampe</author>
      <link>http://blog.micampe.it/articles/2007/01/06/a-little-django-feature-nobody-tells-you-about</link>
      <category>English</category>
      <category>django</category>
      <category>web</category>
      <category>python</category>
      <trackback:ping>http://blog.micampe.it/articles/trackback/1071</trackback:ping>
    </item>
    <item>
      <title>A tale of Pylons, Python and FastCGI on Dreamhost</title>
      <description>&lt;p&gt;&lt;a href="http://pylonshq.com"&gt;Pylons&lt;/a&gt; is a &lt;a href="http://wsgi.org"&gt;WSGI&lt;/a&gt; based Python Web Framework that tries to combine the
best ideas from other existing frameworks and does so by glueing togheter
existing projects and allowing to swap its components at will and easily extend
its capabilites with other WSGI components.&lt;/p&gt;

&lt;p&gt;I will explain here how I installed it and a demo application on &lt;a href="http://www.dreamhost.com/r.cgi?195290"&gt;Dreamhost&lt;/a&gt;
and how to avoid the single problem I had.&lt;/p&gt;

&lt;p&gt;This procedure isn&amp;#8217;t specific to either Dreamhost or Pylons, it can be used
anywhere and for any WSGI compliant application if you don&amp;#8217;t have root access
to install new modules or if you want to keep the system wide installation
clean and add modules just for a single user.&lt;/p&gt;&lt;p&gt;The installation procedure is quite easy and mostly automated, start by running
these commands from your home directory on the remote shell:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir bin
wget -P bin http://peak.telecommunity.com/dist/virtual-python.py
python2.4 bin/virtual-python
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;#8217;s important that you use &lt;code&gt;python2.4&lt;/code&gt; here, not just &lt;code&gt;python&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This command will create some directories and symlinks on your home directory
that will result in you little private Python environment, where you have the
power to install new packages at will. To make the illusion more real, you
should then add &lt;code&gt;~/bin&lt;/code&gt; in front of your &lt;code&gt;$PATH&lt;/code&gt;, so that you can just type
&lt;code&gt;python&lt;/code&gt; in the shell and the interpreter used will be your private one instead
of the global one.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export PATH="~/bin:$PATH"    # add this to ~/.bash_profile
wget -P bin http://peak.telecommunity.com/dist/ez_setup.py
python bin/ez_setup.py
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will install the &lt;code&gt;easy_install&lt;/code&gt; script that you will use to install Pylons
and other Python modules you can find on the &lt;a href="http://cheeseshop.python.org/pypi"&gt;Cheese Shop&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;easy_install Pylons
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will install the Pylons framework and all its dependencies. To test the
installation we will use the &lt;a href="http://pylonshq.com/docs/quick_wiki.html"&gt;QuickWiki&lt;/a&gt; demo application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;easy_install QuickWiki
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will install the application and some more dependencies. The next step is
creating the configuration file for the application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;paster make-config QuickWiki wiki.ini
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this file you only have to change the database URI, for this test we will
use SQLite:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sqlalchemy.dburi = sqlite:///quickwiki.db
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And setup the application to create the database:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;paster setup-app wiki.ini
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then we need a script to start the application. Pylons, as every WSGI
application can be deployed in a number of different ways, but the only one you
can use on Dreamhost is FastCGI. At this point, every tutorial around the web
will tell you to use &lt;a href="http://www.saddi.com/software/flup/"&gt;flup&lt;/a&gt; and then there will be pain and hair pulling
because you won&amp;#8217;t understand why it&amp;#8217;s not working (unless you ask &lt;a href="http://blogs.flup.org/page/asaddi"&gt;Allan
Saddi&lt;/a&gt; to check your setup, that is. Thank you Allan!).&lt;/p&gt;

&lt;p&gt;The solution is to use the supposed to be obsolete &lt;a href="http://svn.saddi.com/py-lib/trunk/fcgi.py"&gt;fcgi.py&lt;/a&gt; script, so let&amp;#8217;s
install it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;wget -P lib/python2.4/site-packages http://svn.saddi.com/py-lib/trunk/fcgi.py
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We said we needed a script, so put this in a file named &lt;code&gt;server.fcgi&lt;/code&gt; in your
web directory and make it executable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/home/username/bin/python

from paste.deploy import loadapp
from fcgi import WSGIServer

app = loadapp('config:/home/username/www.yourdomain.net/wiki.ini')
server = WSGIServer(app)
server.run()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will need to correct the paths here, as we need them to be absolute.&lt;/p&gt;

&lt;p&gt;Now when you point at this file in a web browser you should see the QuickWiki
FrontPage.&lt;/p&gt;

&lt;p&gt;The problem with flup (as found by Allan itself) is that it takes too long to start and it seems that FastCGI on Dreamhost will wait for a maximum of 3 seconds. &lt;code&gt;fcgi.py&lt;/code&gt; instead is simpler (doesn&amp;#8217;t do thread pooling for example) and therefore faster to start. This means that when the server is under heavy load there could still be problems, but it has been pretty reliable.&lt;/p&gt;

&lt;p&gt;The best solution to this problem would be &lt;a href="http://www.mems-exchange.org/software/scgi/"&gt;SCGI&lt;/a&gt; support by Dreamhost, if you already are a customer, go &lt;a href="https://panel.dreamhost.com/index.cgi?tree=home.sugg&amp;amp;search=scgi"&gt;vote for it&lt;/a&gt;!&lt;/p&gt;</description>
      <pubDate>Sun, 26 Nov 2006 08:01:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:8a140a5f-4804-415f-91f2-b2bd554b87af</guid>
      <author>micampe</author>
      <link>http://blog.micampe.it/articles/2006/11/26/a-tale-of-pylons-python-and-fastcgi-on-dreamhost</link>
      <category>English</category>
      <category>python</category>
      <category>pylons</category>
      <category>wsgi</category>
      <category>fastcgi</category>
      <category>dreamhost</category>
      <category>django</category>
      <category>turbogears</category>
      <trackback:ping>http://blog.micampe.it/articles/trackback/1067</trackback:ping>
    </item>
  </channel>
</rss>
