DEV Community

M.Ark
M.Ark

Posted on

Creating a new project in Python Django

First, we need a dedicated directory on our computer to store the code. This can live anywhere but for convenience, if you are on a Mac, we can place it in the Desktop folder. The location really does not matter; it just needs to be easily accessible.
$ cd ~/Desktop
$ mkdir code && cd code
This code folder will be the location for all the code in this project.
The next step is to create a
dedicated directory for our library site, install Django via Pipenv, and then enter the virtual environment using the shell command. You should always use a dedicated virtual environment for every new Python project as shown below.
$ mkdir library && cd library
$ pipenv install django~=3.1.0
$ pipenv shell
(library) $
Pipenv creates a Pipfile and a Pipfile.lock within our current directory. The (library) in parentheses before the command line shows that our virtual environment is active.
A traditional Django website consists of a single project and one (or more) apps representing
discrete functionality. Let’s create a new project with the startproject command. Don’t forget to include the period . at the end which installs the code in our current directory. If you do not include the period, Django will create an additional directory by default.
From the command line .
(library) $ django-admin startproject config .
Django automatically generates a new project for us which we can see with the tree command.
Our Python project has different files. And here are their meanings and functions.
init.py is a Python way to treat a directory as a package; it is empty
• asgi.py stands for Asynchronous Server Gateway Interface and is a new option in Django 3.0+
• settings.py contains all the configuration for our project
• urls.py controls the top-level URL routes
• wsgi.py stands for Web Server Gateway Interface and helps Django serve the eventual web pages
• manage.py executes various Django commands such as running the local web server or creating a new app.
Run migrate to sync the database with Django’s default settings and start up the local Django web server
To run migrate, we runt the following command from our terminal;
Python manage.py run migration
Python manage.py migrate

To check our new website from the browser, we run
python manage.py runserver to start our server.

Open a web browser to http://127.0.0.1:8000 to confirm our project is successfully installed.

Top comments (0)