$ uname -a Linux pokeybill 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1 (2015-05-24) x86_64 GNU/Linux $ python --version Python 2.7.9
Django Installation and usage synopsis:
To install Django, you can use pip which will come standard with Python versions 2.7.9 and 3.4+. The version number is optional.
Install Django : $sudo pip install django[==<version #>]
Check Version : $django-admin –version
The django-admin startproject command will create a directory named after your project in the execution directory and populate it with various core project files.
Create a Project : $django-admin startproject <project name>
Initial Project Folder contents :
project_folder$ ls manage.py project_folder project_folder$ ls project_folder/ __init__.py settings.py urls.py wsgi.py
Project management functions are performed using the manage.py file :
project_folder$ python manage.py Type 'manage.py help <subcommand>' for help on a specific subcommand. Available subcommands: [auth] changepassword createsuperuser [django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runfcgi shell showmigrations sql sqlall sqlclear sqlcustom sqldropindexes sqlflush sqlindexes sqlmigrate sqlsequencereset squashmigrations startapp startproject syncdb test testserver validate [sessions] clearsessions [staticfiles] collectstatic findstatic runserver
You will need an administrative user with which to login and manage your project using the Django web interface, this is done using manage.py :
project_folder$ python manage.py createsuperuser Username (leave blank to use 'bill'): pokeybill Email address: firstname.lastname@example.org Password: Password (again): Superuser created successfully.
There isn’t much to manage until you create some content, so we will revisit the administrator login later. Django projects consist of Django applications. An app will contain data models, templates, views, and other files which will define its functionality and appearance. From the main project folder : python manage.py startapp sample_application . This will create a new folder with the following initial contents :
project_folder$ ls sample_application/ admin.py __init__.py migrations views.py models.py tests.py
Every application must be registered in the project’s settings.py file, which is contained in the project subfolder :
""" Django settings for passages project. Generated by 'django-admin startproject' using Django 1.8. For more information on this file, see https://docs.djangoproject.com/en/1.8/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.8/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'g#%%%%%%%*u)3*oXXXXXXX(7oa+xtYYYYYcrx-30h@!qorw9c' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS =  # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'sample_application' ) ...
Django settings.py documentation – you should get familiar with these settings, keep in mind the values in this file should not be altered at runtime, treat these as constants defined in settings.py.
Databases are created and managed using Models, which are essentially classes containing the various field definitions. These are contained in models.py and defined as in the example below :
from django.db import models class SampleItem(models.Model): title = models.CharField(max_length=100) description = models.TextField() amount = models.IntegerField()
When changes are made to models.py, they must be migrated in order to be applied to the database structure. When a project is initially created, a first migration must be run to create the core database structures. Migrations are made using the makemigrations manage.py parameter; run from the main project folder :
project_folder$ python manage.py makemigrations Migrations for 'sample_application': 0001_initial.py: - Create model Item
This adds the model information to the project_folder/sample_application/migrations/ directory, in the form of a model file. These changes must subsequently be applied. A list of all migrations can be viewed as follows :
project_folder$ python manage.py migrate --list admin [ ] 0001_initial auth [ ] 0001_initial [ ] 0002_alter_permission_name_max_length [ ] 0003_alter_user_email_max_length [ ] 0004_alter_user_username_opts [ ] 0005_alter_user_last_login_null [ ] 0006_require_contenttypes_0002 contenttypes [ ] 0001_initial [ ] 0002_remove_content_type_name itemloader [ ] 0001_initial sessions [ ] 0001_initial
The brackets will be filled with an X when the migrations have been applied. Apply them using manage.py‘s migrate parameter :
project_folder$ python manage.py migrate Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, itemloader, auth, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying itemloader.0001_initial... OK Applying sessions.0001_initial... OK project_folder$ ls db.sqlite3 sample_application manage.py project_folder project_folder$ python manage.py migrate --list admin [X] 0001_initial auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 contenttypes [X] 0001_initial [X] 0002_remove_content_type_name itemloader [X] 0001_initial sessions [X] 0001_initial
Here we can see a database was initialized in the file db.sqlite3 and, when listed, the migrations are marked as applied. Models must also be registered in the app’s admin.py file to allow modification via web login as the site administrator. To register your model, edit the app’s admin.py file :
from django.contrib import admin from .models import Item admin.site.register(Item)
Each individual model class would need to be registered in the application’s admin.py, which makes it available to the application and administrative portal. The example above will cause issues if the item is already registered, there is a helpful function decorator which can assist with registering your models. The example below uses the django.contrib.admin.register decorator to register the model in question when invoking the …admin.ModelAdmin child class :
from django.contrib import admin from .models import AppUser @admin.register(AppUser) class PlayerAdmin(admin.ModelAdmin): list_display = ['username','ip','about']
If the model has not been registered in one of the methods described, errors will be raised in the makemodel routine. The /admin/ site is highly customizable, as always the documentation is very helpful.
Bring up the wsgi server :
project_folder$ python manage.py runserver Performing system checks... System check identified no issues (0 silenced). May 06, 2016 - 22:53:44 Django version 1.8, using settings 'project_folder.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Now the administrative login portal should be up and available using the URL http://127.0.0.1:8000/admin/ (http://localhost:8000/admin/). Logging in presents you with a dashboard and a few options.
Once logged in, you are able to add database entries, and manage other aspects of the project.
At this point, your project is ready for content. Django applications deliver web content in the form of views, which are managed in an app’s views.py file. Views will use HTML templates and static resources to build the actual web pages that will be presented by the server. [Templates, apache mod_wysgi, proxy setups, and Static content requirements to come]