Table of contents
1.
Introduction
2.
Creating a project in Django 
3.
The Project Structure
4.
Setting up your project
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

Creating a project in Django

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Django is a python based free and open-source web framework that follows the model–template–views architecture, and It also enables the rapid development of secure and maintainable websites. Moreover, Django is versatile and can be used to develop any website, say its content management system, and avoid many security mistakes by the developers by enabling protection against vulnerability. Furthermore, it is portable and does not restrict developers to any particular server platform. 

In this article, we will look at creating a project in Django. So without any further ado, let's get started to create our first Django project.

Creating a project in Django 

Before getting started with the following steps, makes sure that you have installed the Django and your basic setup is complete if you haven’t installed Django, then go through the following blog about Django Basic for creating a project in Django.

If you have windows or Linux then open the command prompt and type the following command in the cmd

django-admin startproject demoproject  


This command would create a directory with the name “demoproject” with the following structure

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


In the viewfinder of ide, the folder structure would look like this

The Project Structure

Let us first discuss the project structure in detail and understand the files that we got by executing the command above.

The “demoproject” folder has two elements

Manage.py- this file is a kind of local Django admin that helps the project to interact with your command line. To get a full list of the commands accessible via manage.py you use the following command.

$ python manage.py help


It will open a list of all command 

The “demoproject” subfolder- this folder contains all the actual python packages of your project. It contains the following files-

__intit__.py:  it is a python package.

settings.py: it contains all the settings related to your project.

urls.py: All links of project and function calls

wsgi.py: if you need to deploy your project over WSGI.

Learn more, wget command in linux

Setting up your project

Create a new file views.py inside the subfolder named “demoproject” and copy the following code in the file.

from django.http import HttpResponse
# Defining a function
def demo (request): 
    return HttpResponse("Hello Ninja")
You can also try this code with Online Python Compiler
Run Code


Open the urls.py and your entry for above code

from demoproject.views import demo
You can also try this code with Online Python Compiler
Run Code


Add entry in url field inside urlpatterns

path('demoproject/',demo),
You can also try this code with Online Python Compiler
Run Code


Now run the server

Open command prompt and change  the working directory to env_site by using the following command 

$ cd env_site
You can also try this code with Online Python Compiler
Run Code


Once we are in the env directory use the below commands to activate the virtual environment

$ cd Scripts
$ activate
You can also try this code with Online Python Compiler
Run Code


Change the working directory to demoproject -

$ cd ..
$ cd demoproject
You can also try this code with Online Python Compiler
Run Code


Start the server

$ python manage.py runserver

Open the browser and type this url- 

http://127.0.0.1:8000/demoproject/

Output:

Frequently Asked Questions

  1. How to create a project directory in Django?
    To create a project directory open the cmd and type the following code
    django-admin startproject demoproject
     
  2. What is Django?
    Django is a free and open-source Python-based web framework that employs the Model–template–views design pattern.
     
  3. What are some companies that use Django?
    Instagram, DISCUS, Mozilla Firefox, YouTube, Pinterest, Reddit, and other companies use Django.

  

Key Takeaways

In this article we discussed about creating a project in Django and also implemented a demo project to develope the understanding of django. We briefly discussed about the folder structure of the project along with some important commands.

You can also refer to Django TemplatesDjango Forms and CRUD operations in Django.

Happy Learning!

Live masterclass