Django

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 management functions are performed using the manage.py file :

Django-admin and manage.py documentation

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 :

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 :

Every application must be registered in the project’s settings.py file, which is contained in the project subfolder :

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 :

Models documentation

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 :

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 :

The brackets will be filled with an X when the migrations have been applied.  Apply them using manage.py‘s migrate parameter :

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 :

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 :

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  :

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.

django_login_example

django_admin_panel

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]