<?php include('HEADER.php'); ?>

<h1>Python Django</h1>

<p>The <a href="https://docs.djangoproject.com/en/">docs</a> are good, including the <a href="https://docs.djangoproject.com/en/1.8/intro/tutorial01/">tutorial</a>. If you find the tutorial exhausting, start with <a href="https://docs.djangoproject.com/en/1.8/intro/overview/">Django at a glance</a>.</p>

<p>On Debian: <code>apt-get install python-django sqlite</code>.</p>

<h2>Starting a Project</h2>

<pre>$ django-admin startproject myproject
$ cd myproject
$ tree
.
├── manage.py
└── myproject
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files
</pre>

<h3>settings.py</h3>

<p>The <code>settings.py</code> includes the database info. It used <a href="http://paulgorman.org/technical/sqlite.php">SQLite</a> by default. When using another database (e.g. MySQL), create an empty database at this point (<code>CREATE DATABASE myproject;</code>).</p>

<p>The <code>INSTALLED_APPS</code> tuple includes defaults like 'sessions'. To create the database tables used by these default apps:</p>

<pre>$ 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</pre>

<h3>Development server</h3>

<pre>$ 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.</pre>

<h2>Adding an App to the project</h2>

<p>Each app is a Python package that follows certain Django conventions.</p>

<pre>$ 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 </pre>

<h3>Creating models</h3>

<p>Edit <code>mypreoject/myapp/models.py</code>:</p>

<pre>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</pre>

<p>Add 'myapp' to the <code>INSTALLED_APPS</code> tuple in <code>settings.py</code>.</p>

<p>Run <code>python manage.py makemigrations myapp</code>. If desired, we can review the <code>myapp.0001_initial</code> plan prior to actually touching the database schema. When ready to actually touch the database schema:</p>

<pre>$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, myapp, auth, sessions
Running migrations:
  Applying myapp.0001_initial... OK</pre>

<h4>Playing with the API</h4>

<pre>$ 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)
&gt;&gt;&gt; from myapp.models import Note, Category
&gt;&gt;&gt; Note.objects.all()
[]
&gt;&gt;&gt; from django.utils import timezone
&gt;&gt;&gt; n = Note(rec_date=timezone.now(), note_text="My first note!", importance=2)
&gt;&gt;&gt; n.save()
&gt;&gt;&gt; n.id
1
&gt;&gt;&gt; Note.objects.all()
[&lt;Note: My first note!&gt;]
&gt;&gt;&gt;
</pre>

<h2>Create an admin user</h2>

<pre>$ python manage.py createsuperuser
Username (leave blank to use 'username'): admin
Email address: admin@example.com
Password: 
Password (again): 
Superuser created successfully.</pre>

<p>Add this to <code>myapp/admin.py</code>:</p>
<pre>admin.site.register(Note)</pre>
<p>Run the dev server, and look at <a href="http://127.0.0.1:8000/admin/">http://127.0.0.1:8000/admin/</a>.</p>

<pre></pre>

<h2>Deployment</h2>

<p>Edit <code>settings.py</code>. Set <code>DEBUG = False</code> and <code>TEMPLATE_DEBUT = False</code>.</p>

<p>Our project code should be deployed outside the webserver document root.</p>

<?php include('../FOOTER.php'); ?>
