<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Making the internet a better place - django</title><link>http://better-inter.net/tag/django/</link><description>Torsten Engelbrecht's Blog</description><atom:link rel="self" href="http://better-inter.net/rss/django/"></atom:link><language>en</language><lastBuildDate>Sun, 12 May 2013 13:09:56 +0000</lastBuildDate><item><title>Enabling CORS in Angular JS</title><link>http://better-inter.net/enabling-cors-in-angular-js/</link><description>&lt;p&gt;I was recently experimenting with building an API with &lt;a class="reference external" href="https://github.com/toastdriven/django-tastypie"&gt;django-tastypie&lt;/a&gt; and make it accessible via CORS, so it can be used from a different host from an &lt;a class="reference external" href="http://angularjs.org/"&gt;AngularJS&lt;/a&gt; app.&lt;/p&gt;
&lt;p&gt;For the Django part it was relatively straightforward. I could have either written my own Middleware, dealing with incoming CORS requests, but decided to use &lt;a class="reference external" href="https://github.com/ottoyiu/django-cors-headers"&gt;django-cors-headers&lt;/a&gt; in the end. Following the instructions in the github repo and adding my host where &lt;a class="reference external" href="http://angularjs.org/"&gt;AngularJS&lt;/a&gt; is hosted to the &lt;tt class="docutils literal"&gt;CORS_ORIGIN_WHITELIST&lt;/tt&gt; setting did enable the Django server to handle CORS.&lt;/p&gt;
&lt;p&gt;With &lt;a class="reference external" href="http://angularjs.org/"&gt;AngularJS&lt;/a&gt; it was a little more tricky, mainly because information is spread all over the web. Beside the fact that I was trying to implement a service using &lt;tt class="docutils literal"&gt;ngResource&lt;/tt&gt; to communicate with the API, the following did enable &lt;a class="reference external" href="http://angularjs.org/"&gt;AngularJS&lt;/a&gt; to send its requests with the appropriate CORS headers globally for the whole app:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
var myApp = angular.module('myApp', [
    'myAppApiService']);

myApp.config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
]);
&lt;/pre&gt;
&lt;p&gt;_&lt;/p&gt;
&lt;p&gt;Just setting &lt;tt class="docutils literal"&gt;useXDomain&lt;/tt&gt; to &lt;tt class="docutils literal"&gt;true&lt;/tt&gt; is not enough. AJAX request are also send with the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;X-Requested-With&lt;/span&gt;&lt;/tt&gt; header, which indicate them as being AJAX. Removing the header is necessary, so the server is not rejecting the incoming request.&lt;/p&gt;
</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Torsten</dc:creator><pubDate>Sun, 12 May 2013 13:09:56 +0000</pubDate><guid>http://better-inter.net/enabling-cors-in-angular-js/</guid><category>d</category><category>j</category><category>a</category><category>n</category><category>g</category><category>o</category><category>,</category><category> </category><category>j</category><category>a</category><category>v</category><category>a</category><category>s</category><category>c</category><category>r</category><category>i</category><category>p</category><category>t</category><category>,</category><category> </category><category>a</category><category>n</category><category>g</category><category>u</category><category>l</category><category>a</category><category>r</category><category>j</category><category>s</category></item><item><title>Migrating to Heroku</title><link>http://better-inter.net/migrating-to-heroku/</link><description>&lt;p&gt;
To cut some personal costs and simply because I was interested in it, I recently moved this blog to &lt;a href="http://www.heroku.com/" target="_blank"&gt;Heroku&lt;/a&gt;, the popular cloud hosting platform.
&lt;/p&gt;
&lt;p&gt;
Since 2011 Heroku officially supports Python deployments. Running on a single dyno it is even for free. Following the  &lt;a href="https://devcenter.heroku.com/articles/django" target="_blank"&gt;official guide to deploy a Django application&lt;/a&gt;, the transition from my VPS to Heroku was pretty straight-forward and easy. Additionally Heroku enforces good practices, like using &lt;a href="http://www.virtualenv.org/" target="_blank"&gt;virtualenv&lt;/a&gt;, and supports more and more popular technologies, like the Python WSGI server &lt;a href="http://gunicorn.org/" target="_blank"&gt;Gunicorn&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Following a short general guide of what had to be done from my side to do the transition:
&lt;/p&gt;
&lt;p&gt;
&lt;ul&gt;
&lt;li&gt;Create a fixture file from your database via the &lt;a href="https://docs.djangoproject.com/en/dev/ref/django-admin/#dumpdata-appname-appname-appname-model" target="_blank"&gt;dumpdata command&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Since I didn't want to add any billing information to Heroku yet, I had to rewrite everything related to sending emails from my web server, to save the incoming contact request in the database instead. That was pretty much just the creation of a new model and a one-liner to save it to the DB instead of sending an email,&lt;/li&gt;
&lt;li&gt;Already using virtualenv, the rest was simply adding or changing requirements. In addition of removing the MySQL bindings for python I added the following libaries:
&lt;p&gt;
&lt;pre&gt;
dj-database-url==0.2.1
django-heroku-memcacheify==0.4
django-pylibmc-sasl==0.2.4
gevent==0.13.8
greenlet==0.4.0
gunicorn==0.17.2
psycopg2==2.4.6
pylibmc==1.2.3
&lt;/pre&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Static files are served via Django directly for the time being, since a Web-Dyno using Python can not serve them the normal way. I will move them to be served by a solution like Amazon S3 in the future. Therefore I added the following to my urlconf:
&lt;p&gt;
&lt;pre&gt;
urlpatterns += patterns(
    '',
    url(r'^media/(?P&lt;path&gt;.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
&lt;/pre&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Running the dyno via Gunicorn with gevent support was a single line in the Procfile:
&lt;p&gt;
&lt;pre&gt;web: gunicorn -k gevent wsgi:application&lt;/pre&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Finally: 
&lt;p&gt;
&lt;pre&gt;git push heroku master&lt;/pre&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
So far I am quite happy with Heroku. I didn't recognize any differences to previous VPS hosting and I don't have to deal with server maintenance anymore, even it was an experience I don't want to miss. Goodbye VPS, welcome Heroku!
&lt;/p&gt;

 </description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Torsten</dc:creator><pubDate>Sun, 17 Feb 2013 11:00:02 +0000</pubDate><guid>http://better-inter.net/migrating-to-heroku/</guid><category>p</category><category>y</category><category>t</category><category>h</category><category>o</category><category>n</category><category>,</category><category> </category><category>d</category><category>j</category><category>a</category><category>n</category><category>g</category><category>o</category><category>,</category><category> </category><category>h</category><category>e</category><category>r</category><category>o</category><category>k</category><category>u</category></item><item><title>django-urli18n</title><link>http://better-inter.net/django-urli18n/</link><description>&lt;p&gt;Even I released django-urli18n already two weeks ago, I didn't find the time to actually announce it. So here it is:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is django-urli18n?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A reusable Django app to display the current activated language in the URL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Features&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;- different ways to show the language in the URL: in the path (for example www.example.com/en/home/) or in a query string (for example www.example.com/home/?lang=en)&lt;/p&gt;
&lt;p&gt;- simple to use and include into new or existing Django projects&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where to get it?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Check out the &lt;a href="https://github.com/Torte/django-urli18n" target="_blank"&gt;project page on Github&lt;/a&gt;.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Torsten</dc:creator><pubDate>Mon, 09 May 2011 02:22:39 +0000</pubDate><guid>http://better-inter.net/django-urli18n/</guid><category>p</category><category>y</category><category>t</category><category>h</category><category>o</category><category>n</category><category>,</category><category> </category><category>d</category><category>j</category><category>a</category><category>n</category><category>g</category><category>o</category><category>,</category><category> </category><category>a</category><category>p</category><category>p</category><category>s</category></item></channel></rss>