Are you new to Django, the popular Python web framework? Do you want to learn how to build a web application from scratch? Look no further! In this article, we will take you on a journey from zero to hero, guiding you through the process of building your first Django project.
What is Django?
Django is a high-level Python web framework that enables rapid development of secure, maintainable, and scalable websites. It provides an architecture, templates, and APIs to build web applications quickly and efficiently. With Django, you can focus on writing code that matters, without worrying about the underlying infrastructure.
Prerequisites
Before we begin, make sure you have:
- Python 3.8 or higher installed on your computer
- A code editor or IDE (Integrated Development Environment) of your choice
- Basic knowledge of Python programming
Step 1: Setting up Django
To start building your first Django project, you need to install Django. You can do this by running the following command in your terminal or command prompt:
bash
pip install django
Once the installation is complete, you can verify that Django is installed by running:
bash
django-admin –version
This should display the version of Django installed on your system.
Step 2: Creating a New Project
To create a new Django project, navigate to the directory where you want to create your project and run:
bash
django-admin startproject myproject
Replace myproject with the name of your project. This will create a new directory with the basic structure for a Django project.
Step 3: Understanding the Project Structure
The Django project structure consists of the following directories and files:
myproject/: The project directorymyproject/__init__.py: An empty file that indicates that the directory should be treated as a Python packagemyproject/settings.py: The project settings filemyproject/urls.py: The project URL configuration filemyproject/wsgi.py: The WSGI (Web Server Gateway Interface) configuration filemyproject/asgi.py: The ASGI (Asynchronous Server Gateway Interface) configuration file
Step 4: Creating a New App
In Django, an app is a self-contained module that provides a specific functionality. To create a new app, navigate to the project directory and run:
bash
python manage.py startapp myapp
Replace myapp with the name of your app. This will create a new directory with the basic structure for a Django app.
Step 5: Configuring the App
In the myapp directory, you will find the following files:
__init__.py: An empty file that indicates that the directory should be treated as a Python packageadmin.py: The admin interface configuration fileapps.py: The app configuration filemodels.py: The data models filetests.py: The tests fileviews.py: The views file
Step 6: Defining Models
In Django, models represent the data stored in the database. To define a model, open the models.py file and add the following code:
python
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
publication_date = models.DateField()
This defines a Book model with three fields: title, author, and publication_date.
Step 7: Creating and Applying Migrations
To create the database tables for the Book model, you need to create and apply migrations. Run the following commands:
bash
python manage.py makemigrations
python manage.py migrate
This will create the database tables for the Book model.
Step 8: Creating Views
Views in Django handle HTTP requests and return HTTP responses. To create a view, open the views.py file and add the following code:
python
from django.shortcuts import render
from.models import Book
def book_list(request):
books = Book.objects.all()
return render(request, ‘book_list.html’, {‘books’: books})
This defines a book_list view that retrieves all Book objects from the database and renders an HTML template.
Step 9: Creating Templates
Templates in Django are HTML files that display data. To create a template, create a new file called book_list.html in the myapp/templates directory and add the following code:
{% extends ‘base.html’ %}
{% block content %}
-
{% for book in books %}
- {{ book.title }} ({{ book.author }})
{% endfor %}
{% endblock %}
This defines an HTML template that displays a list of books.
Step 10: Running the Development Server
To run the development server, navigate to the project directory and run:
bash
python manage.py runserver
This will start the development server, and you can access your application at http://localhost:8000/.
Conclusion
Congratulations! You have successfully built your first Django project. You have learned how to set up Django, create a new project, define models, create views, and create templates. You have also learned how to run the development server and access your application.
This is just the beginning of your Django journey. From here, you can learn more about Django by reading the official documentation, watching tutorials, and building more complex projects. Happy coding!