Web Project Using Django: Get Started

Environment

  • Windows 7 x64 (workstation version)
  • Python 3.5 (Anaconda 3)
  • Git (Version 2.10.1.windows.1)

Before start

Make sure you have not used django in this PC before, otherwise you have to remove old version django before start. Then, you need to get these two things ready:

  • pip
  • psycopg2
  1. pip is already installed when you install python, but you need to upgrade it if you are not use the newest version of python, the reason is django can only be install by the newest pip tool,using command below to upgrade it.

    1
    python -m pip install -U pip
  2. Psycopg2 is a postgreSQL adapter for Python, it seems like JDBC for java (like other ODBC adapters). you need to install it use command:

    1
    pip install psycopg2

Install Django

Run commands below on git bash:

1
2
$ git clone git://github.com/django/django.git
$ pip install -e django/

Note that you need to run these commands in the git bash instead of cmd, or you need to add git into environment variables so that you can use git in cmd

Install PostgreSQL on Windows

This step is easy, download the installer and install it.

One thing you should know is the psql cmd can not use in the windows command prompt before you set the environment variables: add the bin file which include ‘psql.exe’ into the path var, for example: add ‘D:\softwares\PostgreSQL\9.5\bin’ into the system path.

Set up an easy django project

One thing you need to know to get start with django is to read the Official Documentation, it is easy to understand and has a tutorial to help you set up a easy django website.

Creating a project

run command below in a directory you want to put your project in and it will initial a django project:

1
django-admin startproject mysite

As you can see, mysite is my project name, it depends on you. In the mysite directory you will see:

1
2
3
4
5
6
7
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

these are the initial files to create a django webiste, you can find more explanations about them in the official doucumentation. In this time you don’t have to know what they means.

Run a server

Run:

1
python manage.py runserver

to run a django server, and you will see:

1
2
3
4
5
6
7
8
9
10
11
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
January 02, 2017 - 15:50:53
Django version 1.10, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

You can change the port and IP, if you want to show this web in your local area network, type this: python manage.py runserver 0.0.0.0:5001 then other people could visit it by access your ip address and the port 5001, for example, if my ip address in LAN is 192.168.12.96, they can visit it typing 192.168.12.96:5001 in their broswer.

Setup an application in django project

Now let us create an application named query in django:

1
python manage.py startapp query

this will create a folder named query in your project folder, and its directory is like:

1
2
3
4
5
6
7
8
9
query/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

Activiting your app

To include the app in our project, we need to add a reference to its configuration class in the INSTALLED_APPS setting. The QueryConfig class is in the query/apps.py file, so its dotted path is ‘query.apps.QueryConfig’. Edit the mysite/settings.py file and add that dotted path to the INSTALLED_APPS setting. It’ll look like this:

1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
'query.apps.QueryConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

actually, you don’t have to do that, you just need to add query fiedl in INSTALLED_APPS, it also works:

1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
'query',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

then, run command:

1
python manage.py makemigrations query

This command let django include your current installed apps. By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.

Migrations are how Django stores changes to your models (and thus your database schema) - they’re just files on disk. You can read the migration for your new model if you like; it’s the file query/migrations/0001_initial.py.

Then you can add features in your application, and for more information about how to setup a strong and robust app, visit django’s official doc