Django Part 1- Creating a new project

willp11

willp11

Posted on June 1, 2022

Django Part 1- Creating a new project

In this post I will be showing how you can create a new Django project.

This tutorial assumes that you already have Python and pip installed. I am using a Windows machine, it uses the command py to run Python, however on Linux it will be different and most likely you will need to replace py with python3.

First, create a new folder that will hold all our project directories. Inside this folder, create a new virtual environment using the command:

py -m venv venv
Enter fullscreen mode Exit fullscreen mode

This will have created a new directory called venv that contains our new virtual environment. Now activate the venv using the command:

venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

On Linux, the command is:

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

We need to install Django using pip. This will install Django into the new virtual environment that we have setup.
Use the command:

py -m pip install Django
Enter fullscreen mode Exit fullscreen mode

Now we can use the django-admin command line utility to create a new project. We will call the Django project “backend” as later we will write a basic frontend using React. Use the command:

django-admin startproject backend
Enter fullscreen mode Exit fullscreen mode

Now navigate inside the newly created project folder.

cd backend
Enter fullscreen mode Exit fullscreen mode

Inside the new project we will create our first app. During this tutorial, we will be creating a custom user model and therefore we will name the app “users”.

To create the app we will be using the manage.py script that Django provides. We will use script for various functionality when building a project, including starting the development server, creating and running database migrations and more.
To create the app, run the command:

py manage.py startapp users
Enter fullscreen mode Exit fullscreen mode

We need to register the new app inside the base app in the project. To do this, we edit the settings.py file inside the inner backend directory. Have a look inside the file users/apps.py, in there we find a class created by Django called UsersConfig. We need to add this class to the INSTALLED_APPS list inside backend/settings.py.

INSTALLED_APPS = [
    …
    users.apps.UsersConfig
]
Enter fullscreen mode Exit fullscreen mode

Now we want to run the development server to check that the project has been created successfully. Use the command:

py manage.py runserver
Enter fullscreen mode Exit fullscreen mode

It will run the development server at the default port of 8000. Open your browser and navigate to http://127.0.0.1:8000 and you should see the Django default welcome page.

💖 💪 🙅 🚩
willp11
willp11

Posted on June 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related