Django is a free open-source high-level web development framework written in python. Built by experienced developers, it enables rapid, secure, maintainable, and scaleable development. There are ready-made components you can use for fast development without the hassle of creating the wheel.
In this article, you will learn how to install Django in a virtual environment, how to create an application, and the application structure.
Getting started
Before moving on there is only one requirement which is python, you must have to create and run Django-based web applications. You can download and install python from python.org.
Let's dive in and install Django. We will install Django in a virtual environment. A virtual environment is just a Python virtual environment that isolates the python interpreter, packages, libraries, and scripts installed into it from other virtual environments and from the system. It basically allows you to run different python applications with different versions of python interpreters and packages.
Creating virtual environment
open your (terminal/command prompt)
and create a new directory
$ mkdir first-app
Change working directory
$ cd first-app
Create virtual environment with venv
, a python module which ships with python when you install it. you didn't need to install it by your self.
$ python3 -m venv venv
it will create virtual environment with name venv
.
Activate virtual environment
$ source venv/bin/activate
You will see environment name in your terminal before prompt
(venv) $
Install Django, again we use pip package manager to install tools which comes with python.
(venv) $ pip install django
It will install django now we can create our application
##Creating application
(venv) $ django-admin startproject first_app .
django-admin is the Django command line utility tool to perform administrative tasks. In the command above we are telling him to start the project with name first_app and .
will tell him to create the project in the current directory, otherwise it will create a new directory with the project name.
The project is been created and the directory tree will look something like this.
โโโ first_app
โ โโโ asgi.py
โ โโโ __init__.py
โ โโโ settings.py
โ โโโ urls.py
โ โโโ wsgi.py
โโโ manage.py
โโโ venv
we will start our server by running the following command. Make sure your virtual environment is still active!
(venv) $ python manage.py runserver
Output will be something like this
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s).
Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 11, 2022 - 20:26:42
Django version 4.1.2, using settings 'first_app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Now our server is running on localhost at port 8000, but we are seeing a warning, which is telling to apply the migration. when create project with django-admin there are migrations which comes with it and we need to apply those to run things smoothly because this is required.
Quite the server with CONTROL-C as mentioned and apply migrations.
(venv) $ python manage.py migrate
Output will be something like this
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 admin.0003_logentry_add_action_flag_choices... 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 auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
we have successfully run the migrations run the server again.
(venv) $ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
October 11, 2022 - 20:35:28
Django version 4.1.2, using settings 'first_app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
This time we did not get the migrations warning and now you can visit the address http://127.0.0.1:8000/
in your browser and you will see the default Django server page.
lets create a admin user and open django admin panel
(venv) $ python manage.py createsuperuser
you will be asked to enter username
, email
, password
and confirm password
Username (leave blank to use 'umair'): admin
Email address: admin@app.com
Password:
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
open http://localhost:8000/admin/login/
in your browser and login the credentials you created.
Congratulations ๐ฅณ you creating your first Django web application.
That will be for this tutorial. stay tuned for the next one.