Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

How to Create a Django Project

4 min read

Django is one of the most popular open source full-stack web development Framework written in Python. Django is a framework for perfectionists with deadlines, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel.

To know more about Django read: Django – Web Framework For Perfectionists

In this article, we will go over the steps for creating and running a Django Project.

Prerequisites

Before you begin, make sure you have Python and Django installed on your system. You can install Django using pip, the Python package manager. Open your terminal and run the following command:

pip install django

Creating A Django Project

A project is a collection of settings for an instance of Django including database configuration, Django-specific options, and application-specific settings.

Before creating a Django project make sure you have Python and Django installed in your machine if not go through How To Install Django

It is recommended to create projects in virtual environments though it's optional using them will save you from a lot of headaches in the future. Read the following article to know why virtual environments are so important: How To A Create Virtual Environment for Python

Note that, I am assuming that you have activated your virtual environment where your Django installation exists.

Next, run the below command.

django-admin startproject mysite

This command will invoke the django-admin.py script, which will set up a new Django project called mysite. A Django project name can be composed of numbers, letters, or underscores. A project name cannot start with a number, it can only start with a letter or underscore in addition special characters and spaces aren't allowed anywhere in a project name.

You’ll now notice within your workspace there is a directory set to the name of your new project, mysite. Inside the directory, there is another directory with the same name as the project and a python script named manage.py

Inside the second level project directory, there are another 4 Python scripts. So the default Django project can be illustrated as follows.

mysite/   manage.py   mysite/      __init__.py      settings.py      urls.py      wsgi.py

__init__.py -  a blank Python script whose presence indicates to the Python interpreter that the directory is a Python package. This file allows Python packages to be imported from the directories they are present.

settings.py - Contains the configuration settings for the Django project.

urls.py - Contains URL patterns for the Django project.

wsgi.py - Contains WSGI configuration properties for the Django project. Basically, Python script used to help run your development server and deploy your project to a production environment.

manage.py -  a command-line utility that lets you interact with your Django project in many ways. This script is responsible for all project specific tasks.

Note that, there are two directories with the project name, this might seem confusing at first however you can always change the name of the outer directory which occasionally referred to as the Base Directory. The second level project directory is hardcoded in some of the Django specific files so better not mess with it.

Running A Django Project

Now that we have created a Django project we can see it in action in the browser. Django comes with a built-in web server for development. Navigate to the Base directory where manage.py is, and run the below command.

python manage.py runserver

Ignore all the migration errors for the moment, Now open your preferred browser and go to http://127.0.0.1:8000/ If everything went right you should see the default the page of Django.Starting A Django Project

To stop the server press ctrl + c in the terminal window. Running the application on a different port is also possible you just need to pass the port number along with the command.

# Run the development server on the local address and port 4345 (http://127.0.0.1:4345/)
python manage.py runserver 4345

# Run the dev server on the 96.126.104.88 address and port 80 (http://96.126.104.88/)
python manage.py runserver 96.126.104.88:80

Conclusion

Creating a Django project is a straightforward process that involves a few simple steps. With Django's powerful tools and conventions, you can quickly build and deploy web applications with ease. From defining models to creating views and templates, Django provides a robust framework for developing dynamic and feature-rich web applications.

In this tutorial, you learned how to create a new Django project, configure database settings, run migrations, and start the development server. As you continue to work on your project, you can explore Django's documentation and ecosystem to leverage its various features and libraries for building even more complex web applications.


DJANGO

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read