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
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.
1python -m pip install -U pipPsycopg2 is a postgreSQL adapter for Python, it seems like JDBC for java (like other ODBC adapters). you need to install it use command:
1pip install psycopg2
Install Django
Run commands below on git bash:
|
|
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:
|
|
As you can see, mysite
is my project name, it depends on you. In the mysite
directory you will see:
|
|
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:
|
|
to run a django server, and you will see:
|
|
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:
this will create a folder named query in your project folder, and its directory is like:
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:
actually, you don’t have to do that, you just need to add query fiedl in INSTALLED_APPS, it also works:
then, run command:
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