django reverse() url lookups

25 May 2008

There is a useful django template tag :

{% url app.views.viewfunc %}

which is the same as:

reverse('app.views.viewfunc')

that should lookup through your urls.py to find the url pattern that matches that view. it should then return the formatted URL. this allows you to configure all of your urls in your urls.py file(s) and keep your templates

this often does not work if your reg-ex is complicated eg.

r'^track/?(?P<slug>.+)$'

where there may or may not be a /
and there may or may not be a slug

There is simply no way that it can reverse that reg-ex and figure out what the URL should/could be.

since using {% url ... %} is very useful the best solution I think is to keep your reg-ex simpler and to use multiple confs for the multiple situations. and then NAME them and refer to the name rather than the view in {% url .. %} and reverse()

urlpatterns = pattern('',

    #  track/q=XYZ<br />        url(r'^ticker/$',<br />         view = 'desk.views.track',<br />            name = 'track_detail'<br />     ),

    # track/XYZ<br />       url(r'^track/(?P<slug>.+)$',<br />          view = 'desk.views.track',<br />            name = 'track_detail_by_url'<br />      ),<br />    )

<form action="{% url track_detail %}"><br />    <input type="text" name="q" /><br />    </form>

but I still can't get this to work:
link to the track

also, its django policy that templates should not raise exceptions. they fail silently and return "". I suppose that's because the "designers" freak every time they see an exception.

but in this case I find it very troublesome so in defaulttags.py I modified it, line 369:

except NoReverseMatch:<br />        assert False, "didn't match reverse view_name:%s" % (self.view_name)<br />      #return ''

to simply fail messily


  • more posts in tech notes
    • django.db.utils.DatabaseError: relation "django_content_type" does not exist

      p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco} I was getting an error while running unit tests and the test database was failing to be created.django.db.utils.DatabaseError: relation "django_content_type" does not existeventually found the problem. &nbsp;I had the following in a file that was imported to a models.py:hrd_type = ContentType.objects.get_for_model(HttpReferrerDomain)The idea being that it can just set the var when it loads up and after that its always there. &nbsp;But this means for creating a fresh database, the db is ...

    • django has two classes called ValidationError

      There is one in django.core.exceptions and one in django.forms.util Using that space age IDE Eclipse I have to say I'm enjoying how much time I've saved just going shift-command-O to organize and resolve all of my imports. &nbsp;But today I've just lost a few hours due to my ok-ing the wrong class. Quite mysterious it was, I raised a ValidationError (core exceptions one) in my form's clean() and watched as the try: except ValidationError: in django's full_clean() completely ignored my ...

    • GDAL fails to build: `.rodata' can not be used when making a shared object; recompile with -fPIC

      &nbsp; libtool: link: g++ -shared -nostdlib /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtbeginS.o .libs/libgdal.la.lnkscript &nbsp;-L/usr/local/lib /usr/local/lib/libgeos_c.so /usr/local/lib/libgeos.so /usr/local/lib/libexpat.so -L/usr/lib -lpq -lrt -ldl /usr/lib/libcurl.so -lssl -lcrypto -lz -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../.. -lstdc++ -lm -lc -lgcc_s /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crtn.o &nbsp; &nbsp; &nbsp; &nbsp; -Wl,-soname -Wl,libgdal.so.1 -o .libs/libgdal.so.1.13.2 /usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC /usr/local/lib/libz.a: could not read symbols: Bad value collect2: ld returned 1 exit status make[1]: *** [libgdal.la] Error 1 make[1]: Leaving directory `/home/crucial/tmp/gdal-1.6.2' ...

    • installing MySQLdb on Ubuntu (mysql-python)

      MySQLdb is the python support bindings for MySQL. &nbsp;Not that the name would lead you to beleive that. Its sourceforge page calls it http://sourceforge.net/projects/mysql-python/ which makes more sense. you need setuptools, which you usually already have: &nbsp;&nbsp; &nbsp;sudo aptitude install python-setuptools You need MySQL-devel to compile, but its not called that, its called: libmysql++-dev on Ubuntu &nbsp;&nbsp; &nbsp;sudo apt-get install libmysql++-dev download MySQLdb itself from: &nbsp;&nbsp; &nbsp;http://sourceforge.net/projects/mysql-python/ &nbsp;&nbsp; &nbsp;# the version you download will be more recent &nbsp;&nbsp; &nbsp;tar xfz ...

    • Full index for tech notes
Backwardation : Version 12"
coltan and cassiterite Timeblind : Solar Life Raft Ingredients