Few notes on learning Django


Almost a weak ago I started learning Django. Django is framework for developing websites written in Python (from programmer point of view Django is just a Python module). Reason why I started with it is that I kinda like developing websites. So before I even started with learning Django, I was choosing between two options – Django and Ruby on Rails. I chose Django because as you can see in my blog I am pretty familiar with Python.

My Django learning process was based on reading an excellent website called Django Book. After reading first seven chapters I got familiar with basics in Django (views, models, templates, forms, admin, urls are pretty much all of them). I discovered that Django is very powerful tool and you don’t have to write a lot to achieve simple thing (maybe the hardest part is building a template).

So after reading couple of chapters I was able to build a working example which saves data about movie in database from web form and shows all entries in database (these are two web pages). In this example I use ModelForm which is not presented in website Django Book and implement simplified function which handles form.

Source code from main files of example is posted below (you can also see it on github).

[python title=”Izvorna koda za models.py”]
from django.db import models

class Movie(models.Model):
title = models.CharField(max_length=30)
genre = models.CharField(max_length=30)
runtime = models.IntegerField()
imdb_link = models.URLField()

def __unicode__(self):
return self.title
[/python]
[python title=”Izvorna koda za forms.py”]
from django import forms
from movie.models import Movie

class AddMovieForm(forms.ModelForm):
class Meta:
model = Movie
[/python]
[python title=”Izvorna koda za views.py”]
from django.shortcuts import render_to_response
from django.http import HttpResponse
from movie.models import Movie
from movie.forms import AddMovieForm

def add_movie(request):
form = AddMovieForm(request.POST or None)
if form.is_valid():
movie = form.save()
return HttpResponse(‘Successfully added!’)

return render_to_response(‘add_movie_form.html’, dict(form=form))

def show_movies(request):
movies = Movie.objects.all()
return render_to_response(‘show_movies.html’, dict(movies=movies))
[/python]
[python title=”Izvorna koda za urls.py”]
from django.conf.urls import patterns, include, url

urlpatterns = patterns(‘movie.views’,
(r’^add-movie/$’, ‘add_movie’),
(r’^show-movies/$’, ‘show_movies’),
)
[/python]
[html title=”Izvorna koda za add_movie_form.html”]
<!DOCTYPE html>
<html>
<head>
<title>Form for adding a movie to database</title>
</head>
<body>
<form action="" method="post">
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit movie">
</form>
</body>
</html>
[/html]
[html title=”Izvorna koda za show_movies.html”]
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table border="1">
{% for movie in movies %}
<tr>
<td>{{ movie.title }}</td>
<td><{{ movie.genre }}/td>
<td>{{ movie.runtime }}</td>
<td>{{ movie.imdb_link }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
[/html]