0 Today on Django: A Few Lessons Learned

Today I processed my first html form. No full CRUD just yet, but I successfully added a record to the database with my own views and forms. The following is what I learned.

Django Documentation == Puzzle

I don’t mean puzzle in a negative sense. But rather in the sense that you can’t learn a task from one aspect of documentation. Lets say we want to learn how to process a form with some user authentication. There is no particular documentation on that task as a whole. You have to look around and find the relevant pieces relating to the task. In my situation I learned this the hard way. Now lets get to some code.

Organizing Your Code

In Ruby on Rails I was exposed to wonderful directory organization and code organization out of the box. In Django it isn’t as pristine. Django commands do a very small amount of the work for you. Unlike Ruby on Rails however, you can easily access every bit of code like so.


  from myproject.app.blog.models import Post

After the method “from” you have three words daisy-chained by a period. In actuality those three represent ”/myproject/app/blog/models.py”. Pretty cool, eh? I think so. The “import” method pulls in the Post class inside you models.py code. Example of this model below.


 # located in /myproject/app/blog/models.py

class Post(models.Model):

    title = models.CharField(blank=False, max_length=100)
    body = models.TextField(blank=True)
    summary = models.TextField(blank=True)
    blog = models.ForeignKey(Blog)

(All Django directories need an __init__.py file)

It took me some time to realize this, but its certainly helpful to know. Not gonna drone on today, but those are the two (sorry not few) lessons learned while working with Django. Hope it helps some poor web design trying to learn how to build Django apps.