layout | title | description |
---|---|---|
post |
Django quick-start tutorial (Ubuntu) |
How to get up and running with the python Django framework quickly and easily |
I'm just learning to use Django 1.6. There is already a very sufficient official tutorial, which I'm currently following, but in the interests of plurality, I'm going to write my take on this quick-start tutorial below:
To keep this tutorial simple, we're going to leave Django configured to use the default sqlite3 database.
Down the line you will want to use a real database, and you might want to use Python 3.3, keep your Django project in a virtual environment or setup version control, but to keep this tutorial short and to the point, I'm not going to cover any of these topics here.
First we need to install pip and the django libraries:
$ sudo apt-get install python-pip sqlite3 # Install pip (python package manager) and sqlite (database)
$ sudo pip install Django # Install Django
$ python # Check it's installed
>>> import django
>>> django.VERSION
(1, 6, 0, 'final', 0)
After install Django, a python admin script should be in your PATH
called django-admin.py
. Use it to create a project:
$ django-admin.py startproject myproject # create files for a new project called "myproject"
This will create the following project files:
$ tree myproject/ # show the file structure in our new project
myproject/
├── manage.py
└── myproject
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
manage.py
is similar to django-admin.py
and we will be using it to do the rest of the setting up for your new project.
Now change into your new project directory:
$ cd myproject # change to project directory
Database settings are stored in the myproject/settings.py
file. By default, Django is configured to use sqlite3.
You can use manage.py syncdb
at any time to let Django automatically update your database schema for any new models in your app. Run it now to create the database and the core Django tables:
$ python manage.py syncdb # update the database for the app
If you're using sqlite, you'll notice that there is now a new file called db.sqlite3
. This is your database.
$ ls -l # if you're in your top project directory
db.sqlite3 djapp djexperiment manage.py polls
If you like you can inspect the tables that have just been created:
$ sqlite3 db.sqlite3
sqlite> .tables
auth_group auth_user_user_permissions
auth_group_permissions django_admin_log
auth_permission django_content_type
auth_user django_session
auth_user_groups
In Django, an app is a separate concept from a project. The project we create above could potentially contain many apps. We're going to create the "polls" app, just like the official Django tutorial.
So let's create a "polls" app using manage.py
:
$ python manage.py startapp polls
This will create an app directory structure as follows:
$ tree polls/
polls/
├── admin.py
├── __init__.py
├── models.py
├── __pycache__
│ ├── __init__.cpython-33.pyc
│ └── models.cpython-33.pyc
├── tests.py
└── views.py