Getting Started with Django

Installation

Classical

Download from https://www.djangoproject.com/download/. Extract. Then install via

python setup.py install

PIP

$ aptitude install python-pip
$ pip install django

PostgreSQL

See postgresql how to set up a database (schema) and a user.

aptitude install build-essential
apt-get build-dep python-psycopg2
pip install psycopg2

Django Project

Start project.

$ django-admin.py startproject mysite

Start development server.

$ python manage.py runserver

Note: If you run this in a VM (such as this), but want to access it from the host, you need to bind the development server to all interfaces (Link), i.e.

$ python manage.py runserver 0.0.0.0:8000

Start an app.

$ python manage.py startapp myapp

Design models.

$ vim myapp/models.py
from django.db import models

# Create your models here.
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 = models.CharField(max_length=200)
    votes = models.IntegerField()

Add app to project.

$ vim settings.py
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'myapp'
)

Check out generated SQL.

$ python manage.py sql myapp

Create tables.

$ python manage.py syncdb
 
django.txt · Last modified: 2011/09/16 09:59 by volker
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki