Posts Tagged ‘django’

Debugging Django {% url %} tag

Wednesday, February 6th, 2008

When you sometimes notice that {% url %} tag does not work for your named urls, then debugging it is easy. Start the python shell:

import app.views
from django.core.urlresolvers import reverse
reverse('named-view-to-be-tested')

If the view doesn’t work, it will either return NoReverseMatch: Not enough positional arguments passed in exception or url.

Thanks to jodal from #django for the tip.

Django with Git

Friday, January 4th, 2008

I decided to take a peek into Django world and while playing around with tutorial project I decided to use also git for version control.
As basic Django project layout is following:

project/
    __init__.py
    manage.py
    settings.py    # < -- Contains sensitive information
    urls.py

Where settings.py contains sensitive information like usernames/passwords and other data one doesn’t actually want to save in the repository.
But fortunately it’s possible to use external configure file which is included by main configuration file and thanks to Python’s clever exception system, or ImportError exception, we can simply make following modification to settings.py:

diff --git a/project/settings.py b/project/settings.py
index 641c97f..9019d9e 100644
--- a/project/settings.py
+++ b/project/settings.py
@@ -84,4 +84,9 @@ INSTALLED_APPS = (
     'django.contrib.sites',
 )
-
+
+try:
+	import sys
+	from settings_local import *
+except ImportError, e:
+	print "Unable to load local settings: %s" % e
+	sys.exit(1)

Then we create settings_local.py which contains all these defines you do not want to appear inside the repository.