Learning Django From the documentation

After learning Python, decided to learn django 1.5 using windows, first I was using this for db mysql then had trouble connecting it so I went back to sqlite.

findout what's your django version

python -c "import django,sys; print(django.get_version()); print(sys.version)"
python -c "import sys; print(sys.version)"

Download django
unzip django
open cmd

python setup.py install


'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'madindo', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'HOST': 'localhost', # Empty for localhost through domain sockets or '' for localhost through TCP.
'PORT': '', # Set to empty string for default.

create project

django-admin.py startproject madindo

create apps

python manage.py startapp polls


from django.db import models

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __unicode__(self):
return self.title

check sql

python manage.py sql polls
python manage.py validate

sync db for admin

python manage.py syncdb

register apps admin

create admin.py in the folder apps

from django.contrib import admin
from polls.models import Choice, Poll

class ChoiceInline(admin.TabularInline):
model = Choice
extra = 4

class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
inlines = [ChoiceInline]
list_display = ('question', 'pub_date')
admin.site.register(Poll, PollAdmin)

create view

from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. You're at the poll index.")

def detail(request, poll_id):
return HttpResponse("You're looking at poll %s." % poll_id)

def results(request, poll_id):
return HttpResponse("You're looking at the results of poll %s." % poll_id)

def vote(request, poll_id):
return HttpResponse("You're voting on poll %s." % poll_id)

create in apps urls.py

from django.conf.urls import patterns, url
from polls import views

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?Pd+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?Pd+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?Pd+)/vote/$', views.vote, name='vote'),

link urls.py to apps/urls.py by using

url(r'^polls/', include('polls.urls')),

set template in setting.py for admin


copydjango/contrib/admin/templates/admin/base_site.html to/path/to/mysite/templates/admin/base_site.html

set template polls/templates/polls/index.html

{% if latest_poll_list %}
{% else %}

No polls are available.

{% endif %}

to views

set apps/urls.py

url(r'^$', views.IndexView.as_view(), name='index'),

set apps/views.py

class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
def get_queryset(self):
"""Return the last five published polls."""
return Poll.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5]