Wednesday 27 March 2013

Django: Common Errors, variables not appearing

While it's against the django mindset, I have done a few copy-pastes in my time using this framework.

Recently I've been doing some copy-pasting of some modules and found that often I was getting blank lines when retrieving variables.

The problem was that my text editor was fudging with the tab order of some of the lines-- a huge problem in python-based programs. My Meta class was tabbed forward so it was part of the method, not the class. By de-tabbing it, I was able to see my variables as expected.

Sometimes it's the small things.
If such a scenario exists, a better idea that goes along with the DRY principle would be to create one base Form class with the Meta class and have all other classes inherit it appropriately.

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. =/