Getting started of Django 2.0 web application
Django 2.0+ is the big change against 1.x versions of Django. In new version, Few functionalities are changed. For changes look at the official release note. It supports Python 3.4+. Django 1.11 was the last version to support Python 2.7.
Before creating new project we must create one python virtual environment to make separate development environment for the project.
Create Python virtual environment with name
venv. To do that follow this tutorial - setup-python-virtual-environment.
Activate Python virtual environment
Now as this Python virtual environment is separate to work on the project so we can install the python packages which won’t affect any other python projects which are running on the system. So lets install required Django web framework to this environment.
pip install Django==2
This above command will install latest Django version 2.x, as of now latest is 2.1
After installation of Django framework, binary django-admin was installed in so whenever you activate this virtual environment django-admin command will be available to execute in Terminal/Command Prompt.
Lets Create new project using this django-admin command.
<venv> # django-admin startproeject demo
It will create project folder with few files to run a project with one home page.
Following folder structure will be created,
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
Inside project folder following two items are created.
demo (sub-folder) — Main app of the django project which contains root url configuration and other settings related files.
__init__.py- Indicate the folder as package.
settings.py- Configurations of the project like database connetion details, template files, static files and many more.
urls.py- It contains root urls of the project.
wsgi.py- This file is for deploying this project using wsgi related technologies like uWSGI and gunicorn.
manage.py - This file has environment declaration of the project so project can run using this file. And also django commands can be executed through this script.
Now lets execute few commands to run the project first time.
FIrst, Lets migrate database using following command, which will create database and create tables according to models defined in pre built django apps like auth, session and few others.
<venv> # python manage.py migrate
Following will be the output of above command.
Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... 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 auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying sessions.0001_initial... OK
Now database is ready so we can run the project using following command.
<venv> # python manage.py runserver 0:8000
Following will be printed in shell,
Performing system checks... System check identified no issues (0 silenced). August 28, 2018 - 17:17:53 Django version 2.0.4, using settings 'demo.settings' Starting development server at http://0:8000/ Quit the server with CONTROL-C.
0:8000 is optional argument for telling runserver command to run project on all the IPs which exists in your system with port
8000. If you won't give this argument as follows:
<venv> # python manage.py runserver
Following will be printed in shell,
Performing system checks... System check identified no issues (0 silenced). August 28, 2018 - 17:17:03 Django version 2.0.4, using settings 'demo.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Now hit this url http:localhost:8000 in browser and Default page of Django will be displayed:
That’s it in this article, See you all in next article for more detailed tutorials for Django.
Thanks for reading this article.
Originally published at http://www.aaravtech.com.