DEV Community

Cover image for Getting started with Django project
Elizabeth Ng'ang'a
Elizabeth Ng'ang'a

Posted on

Getting started with Django project

Introduction

Django is a robust and versatile Python framework designed to simplify web development. However, how you start your Django project can significantly impact its scalability, maintainability, and performance. This guide provides a comprehensive, step-by-step walkthrough to help you start your Django project the right way, ensuring a solid foundation for success and also tries to explain the project settings and configurations.

Django Project Structure

project structure in django is designed to support the Model-View-Template (MVT) architectural pattern, which is Django’s version of the traditional Model-View-Controller (MVC) framework.

Step 1

I created a folder on my desktop to hold my project and named it "WASTE SOTOR".

Step 2

I create a virtual enviroment, since am on windows i used,

python -m venv env
Enter fullscreen mode Exit fullscreen mode

This creates a folder named env that will store all project-specific Python packages.
Later i had to activate the enviroment using;

env\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

This is an image after i have created and activated the virtual enviroment it created a folder named env.
env setup

Step 4

Install Django
I used;

pip install django
Enter fullscreen mode Exit fullscreen mode

This is are the folders that are created after installing Django, they are created on the env folder.

django installation

Step 5

Start a project
I used this since i wanted my project to be called waste_sorter ;

django-admin startproject waste_sorter .
Enter fullscreen mode Exit fullscreen mode

This are the project settings and configurations installed.

Image description
checking if my project was working
I had to run my project using;

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

follow the link provide and you should see this;

Image description

1.init.py- Makes the folder a Python package .
2.settings.py-Contains all configurations: database, apps, templates, static files, etc.
3.urls.py-Controls which page shows whatand also connects URLs to views.
4.asgi.py-Used for advanced or real-time features and also handles asynchronous requests.
5.wsgi.py-Used to connect Django to a web server and handles normal (synchronous) requests.

Step 6

In this case i started my app and i had 2 of them using the command;

python manage.py startapp app_name
Enter fullscreen mode Exit fullscreen mode

here is an image both apps i created;

app
short description of the Django App Components

  1. admin.py: Configuration for the Django admin interface.
  2. apps.py: Configuration for the app itself.
  3. models.py: Contains the database models for the app.
  4. tests.py: Contains tests for the app.
  5. views.py: Contains the request/response logic for the app.
  6. migrations/: Contains database migrations for the app. Registered the apps so that my apps could be recognized ,i opened the settings.py and added the apps on the INSTALLED_APPS.

Image description

writing views and creating urls

this are the codes that i wrote, i had two since the apps are two;

Image description

Image description

Step 7 created Urls for both apps

I created new files and made them "urls.py" under each app.

Image description
connecting both Urls
This is where i had to join bothof the urls that i created to the main project.
This is what it looked like;

Image description

step 8

Adding Templates
This this the folder that shall be kholding all my pages.
Example of one of my pages ;

Image description

Step 9

Checking if the project is Running ;
i used the

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

then follow the link to the browser .For me i got this;

Image description

Conclusion
Starting a Django project the right way sets the foundation for a scalable, maintainable, and efficient web application.The images and step-by-step instructions demonstrate how each component fits together, from the initial runserver check to rendering dynamic templates. Whether you’re building a simple app like "WASTE SOROR" or a complex system, Django’s flexibility and structure empower you to focus on functionality rather than boilerplate.

Top comments (0)