Tue. Mar 28th, 2023

How to get a Django web app up and running.

Step 1

Confirm you have python installed:

python --version

If you have python installed, the above command will return the version you are using, for example:

Python 3.11.1

If you do not have python installed, visit python.org and install for your platform.

Step 2

From the command line, create and navigate into the directory where you want to create the new web app:

cd c:
mkdir django-example
cd django-example

Step 3

Create a python virtual environment inside the new directory:

python -m venv .venv

Step 4

Once the above command has finished (which may take a minute or so), run the following command to activate the virtual environment:

.venv/Scripts/activate

Step 5

Install the latest version of Django from the command line:

pip install django

Step 6

Now we have Django installed, we need to create a new app within our project (projects can contain a number of apps).

From the command line, create a new app within our project called ‘config’ as this intial app will mostly contain files relating to the settings of our project:

django-admin startproject config .

We include the ‘.’ at the end of the command as it prevents creating an external folder like this: /django-example/config/config which you might need to restructure and remove when it comes to deployment.

Once the config app is created, our project structure will look like this:

Lets take a look at what of each of these new files are:

  • config/ is the external folder of your project. It doesn’t matter to Django, so you can actually rename it to whatever you want.
  • config/config/ is the actual Django project folder. It contains the setting files of your project.
  • manage.py is a Python script with the same functionality of django_admin but uses your project’s settings.
  • __init__.py makes config/config a Python package.
  • settings.py is the core file of your project. You can add, modify, or delete variables to change the behaviour of your project.
  • urls.py is the file that defines the URLs of your project.
  • asgi.py and wsgi.py let you deploy your project to a server.

Step 7

Django comes packaged with basic database setup, IF you’ll be using the basic setup its a good idea to run these migrations now:

python manage.py migrate

Step 8

Once the migration has finished fire up our Django web app:

python manage.py runserver

Once the app is up and running you’ll see the following command line info:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 17, 2023 - 08:45:09
Django version 4.1.7, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

You can now visit http://127.0.0.1:8000/ from your browser:

By admin