How to Create a Django Project (Basic)

How to Create a Django Project (Basic)

Step-by-Step guide:

  1. Open IDE
  2. Create New Project
  3. Create Virtual Environment
  4. Install Django
  5. Create Django Project
  6. Run Django Server

Open IDE

Firstly we have to open our IDE (integrated development environment). Here I'm using PyCharm. If you don't have PyCharm, just download it from here.

Create New Project

From PyCharm create a new project.

Select the project location and give a project name. [In the picture the project name is laxleague.

Create Virtual Environment

What is Virtual Environment: A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.

Why do we need it: Imagine a scenario where you are working on two web based python projects and one of them uses a Django 1.9 and the other uses Django 1.10 and so on. In such situations virtual environment can be really useful to maintain dependencies of both the projects.

By default, every project on your system will use these same directories to store and retrieve site packages (third party libraries). How does this matter? Now, in the above example of two projects, you have two versions of Django. This is a real problem for Python since it can’t differentiate between versions in the “site-packages” directory. So both v1.9 and v1.10 would reside in the same directory with the same name. This is where virtual environments come into play. To solve this problem, we just need to create two separate virtual environments for both the projects.The great thing about this is that there are no limits to the number of environments you can have since they’re just directories containing a few scripts.

Virtual Environment should be used whenever you work on any Python based project. It is generally good to have one new virtual environment for every Python based project you work on. So the dependencies of every project are isolated from the system and each other.

How does it work: We use a module named virtualenv which is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.

Source: GeeksForGeeks

Firstly we have to install virtualenv library,

pip install virtualenv

to create Virtual Environment files we have to run this command.

python -m venv .venv

Here the file will be created as .venv (I'm using dot file as by default git will ignore the file and won't add it to the repository, less hassle 😉 )

For activation of the Virtual Environment file, we have to run this command.

.venv\Scripts\activate

For exiting the Virtual Environment just type deactivate white it's activated.

Install Django

For installing Django we have to run,

pip install Django

Create Django Project

For starting the Django project we have to run this command,

django-admin startproject projectname

startproject command will create some files for the Django project,

projectname/
    manage.py
    projectname/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

These files are:

  • The outer mysite/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

Source: Djangoproject.com

Run Django Server

To run the Django project we just need to run a simple command.

python manage.py runserver

if everything you have done is correct then you will see something like this on the console.

Performing system checks…

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

October 09, 2022 - 14:06:53
Django version 4.1, using settings 'projectname.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Leave a Reply