Tuesday, 19 March 2013

Django: Python Unchained

So I've been doing a lot of work with django lately (https://www.djangoproject.com/) and it has been an excellent MVC so far. Python in particular is very powerful and I've had 3+ years of experience with it, as opposed to CakePHP and Ruby on Rails (PHP/Ruby).

However, MVC's are not without their difficulties, even though they do save time and effort. Documentation has always been the #1 issue when using other people's software, and to this day there have only been a couple bits of software I've used with proper docs. Python and KDE are two big names at the top of my head.

I experienced one such django issue for which there wasn't a clear answer to what I was experiencing, and with the interests of newbies in mind, I will document some of those weird django errors in this blog.


Problem: Django is not displaying a view and gives 404 errors, doesn't report URLs that are defined in the urls.py file.

Solution: Every definition of the urlpatterns variable must include a blank string at the start of the tuple parameter you supply.

Wrong:
urlpatterns = patterns(url(r'^index/', index.index),
)

Right: 
urlpatterns = patterns('',
    url(r'^index/', index.index),
)

In hindsight, it would be a simple fix if I knew what pattern() required. I hope this helps at least one other person.

Cheers.


p.s. I *would* like to format code snippets properly, but apparently Blogger is incapable of doing so without some css/javascript tweaking. =/

No comments:

Post a Comment