Django is a web development framework based on Python. Django 1.11 (the latest stable release) requires Python 2.7, 3.4, 3.5 or 3.6 to be installed. Assuming
pip is available, installation is as simple as running the following command. Keep in mind, omitting the version as shown below will install the latest version of django:
$ pip install django
For installing specific version of django, let's suppose the version is django 1.10.5 , run the following command:
$ pip install django==1.10.5
Web applications built using Django must reside within a Django project. You can use the
django-admin command to start a new project in the current directory:
$ django-admin startproject myproject
myproject is a name that uniquely identifies the project and can consist of numbers, letters, and underscores.
This will create the following project structure:
myproject/ manage.py myproject/ __init__.py settings.py urls.py wsgi.py
To run the application, start the development server
$ cd myproject $ python manage.py runserver
Now that the server’s running, visit
http://127.0.0.1:8000/ with your web browser. You’ll see the following page:
By default, the
runserver command starts the development server on the internal IP at port
8000. This server will automatically restart as you make changes to your code. But in case you add new files, you’ll have to manually restart the server.
If you want to change the server’s port, pass it as a command-line argument.
$ python manage.py runserver 8080
If you want to change the server’s IP, pass it along with the port.
$ python manage.py runserver 0.0.0.0:8000
runserver is only for debug builds and local testing. Specialised server programs (such as Apache) should always be used in production.
Adding a Django App
A Django project usually contains multiple
apps. This is simply a way to structure your project in smaller, maintainable modules. To create an app, go to your projectfolder (where
manage.py is), and run the
startapp command (change myapp to whatever you want):
python manage.py startapp myapp
This will generate the myapp folder and some necessary files for you, like
In order to make Django aware of myapp, add it to your
# myproject/settings.py # Application definition INSTALLED_APPS = [ ... 'myapp', ]
The folder-structure of a Django project can be changed to fit your preference. Sometimes the project folder is renamed to
/src to avoid repeating folder names. A typical folder structure looks like this: