DEV Community

Cover image for Django create your first web application πŸ˜€
Umair Mehmood (TheFatBoy)
Umair Mehmood (TheFatBoy)

Posted on

Django create your first web application πŸ˜€

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, 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
Enter fullscreen mode Exit fullscreen mode

Change working directory

$ cd first-app
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

it will create virtual environment with name venv.

Activate virtual environment

$ source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

You will see environment name in your terminal before prompt

(venv) $
Enter fullscreen mode Exit fullscreen mode

Install Django, again we use pip package manager to install tools which comes with python.

(venv) $ pip install django
Enter fullscreen mode Exit fullscreen mode

It will install django now we can create our application

Creating application

(venv) $ django-admin startproject first_app .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

we will start our server by running the following command.
Make sure your virtual environment is still active!

(venv) $ python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

we have successfully run the migrations run the server again.

(venv) $ python manage.py runserver
Enter fullscreen mode Exit fullscreen mode
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.
Enter fullscreen mode Exit fullscreen mode

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.

django-web-application-server-umair-mehmood

lets create a admin user and open django admin panel

(venv) $ python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

open http://localhost:8000/admin/login/ in your browser and login the credentials you created.

django-admin-panel-first-application-umair-mehmood

Congratulations πŸ₯³ you creating your first Django web application.

That will be for this tutorial. stay tuned for the next one.

Top comments (0)