The docs are good, including the tutorial. If you find the tutorial exhausting, start with Django at a glance.
On Debian: apt-get install python-django sqlite
.
$ django-admin startproject myproject $ cd myproject $ tree . ├── manage.py └── myproject ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py 1 directory, 5 files
The settings.py
includes the database info. It used SQLite by default. When using another database (e.g. MySQL), create an empty database at this point (CREATE DATABASE myproject;
).
The INSTALLED_APPS
tuple includes defaults like 'sessions'. To create the database tables used by these default apps:
$ python manage.py migrate Operations to perform: Apply all migrations: admin, contenttypes, auth, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying sessions.0001_initial... OK
$ python manage.py runserver Performing system checks... System check identified no issues (0 silenced). July 23, 2015 - 21:01:21 Django version 1.7.9, using settings 'myproject.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Each app is a Python package that follows certain Django conventions.
$ python manage.py startapp myapp --- bava --- myproject % tree myapp myapp ├── admin.py ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── tests.py └── views.py 1 directory, 6 files
Edit mypreoject/myapp/models.py
:
from django.db import models class Note(models.Model): rec_date = models.DateTimeField('Date Recorded') note_text = models.CharField(max_length=1024) importance = models.IntegerField(default=1) def __str__(self): return self.note_text class Category(models.Model): note = models.ForeignKey(Note); category_name = models.CharField(max_length=64) def __str__(self): return self.category_name
Add 'myapp' to the INSTALLED_APPS
tuple in settings.py
.
Run python manage.py makemigrations myapp
. If desired, we can review the myapp.0001_initial
plan prior to actually touching the database schema. When ready to actually touch the database schema:
$ python manage.py migrate Operations to perform: Apply all migrations: admin, contenttypes, myapp, auth, sessions Running migrations: Applying myapp.0001_initial... OK
$ python manage.py shell Python 2.7.10 (default, Jul 1 2015, 10:54:53) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from myapp.models import Note, Category >>> Note.objects.all() [] >>> from django.utils import timezone >>> n = Note(rec_date=timezone.now(), note_text="My first note!", importance=2) >>> n.save() >>> n.id 1 >>> Note.objects.all() [<Note: My first note!>] >>>
$ python manage.py createsuperuser Username (leave blank to use 'username'): admin Email address: admin@example.com Password: Password (again): Superuser created successfully.
Add this to myapp/admin.py
:
admin.site.register(Note)
Run the dev server, and look at http://127.0.0.1:8000/admin/.
Edit settings.py
. Set DEBUG = False
and TEMPLATE_DEBUT = False
.
Our project code should be deployed outside the webserver document root.
© Paul Gorman