Django Beginners Guide 2025

Django Beginners Guide 2025

Welcome to the Django beginners guide for 2025. This tutorial is designed to help you get started with Django, a high-level Python web framework that enables rapid development of secure, maintainable, and scalable websites. In this guide, we will cover the basics of Django, including installation, project setup, models, views, templates, and URLs. By the end of this tutorial, you will have a solid understanding of how to build a basic website using Django.

Installing Django

To start using Django, you need to install it first. You can install Django using pip, the Python package manager. Here’s how you can do it:

pip install django

Once the installation is complete, you can verify it by checking the Django version:

django-admin --version

Setting Up a New Django Project

After installing Django, you can set up a new project using the following command:

django-admin startproject myproject

This will create a new directory called myproject containing the basic structure for a Django project. The directory will have the following structure:


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

Creating a New Django App

In Django, a project is made up of one or more apps. An app is a self-contained module that provides a set of related functionality. To create a new app, navigate to the project directory and run the following command:

python manage.py startapp myapp

This will create a new directory called myapp containing the basic structure for a Django app. The directory will have the following structure:


myapp/
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    urls.py
    views.py
    migrations/
        __init__.py

Defining Models

In Django, models represent data stored in the database. To define a model, you need to create a class that inherits from django.db.models.Model. Here’s an example of a simple model:


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()

After defining a model, you need to create a database table for it. You can do this by running the following commands:


python manage.py makemigrations
python manage.py migrate

Creating Views

In Django, views are functions that handle HTTP requests and return HTTP responses. To create a view, you need to define a function that takes a request object as an argument and returns a response object. Here’s an example of a simple view:


from django.http import HttpResponse
from .models import Book

def book_list(request):
    books = Book.objects.all()
    output = ', '.join([book.title for book in books])
    return HttpResponse(output)

After defining a view, you need to map it to a URL. You can do this by adding a URL pattern to the urls.py file:


from django.urls import path
from . import views

urlpatterns = [
    path('books/', views.book_list, name='book_list'),
]

Using Templates

In Django, templates are used to separate presentation logic from application logic. To use a template, you need to create a HTML file in the templates directory of your app. Here’s an example of a simple template:


<html>
    <head>
        <title>Book List</title>
    </head>
    <body>
        <h1>Book List</h1>
        <ul>
        {% for book in books %}
            <li>{{ book.title }}</li>
        {% endfor %}
        </ul>
    </body>
</html>

To use this template in a view, you need to render it and pass the required data:


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})

Conclusion

In this tutorial, we covered the basics of Django, including installation, project setup, models, views, templates, and URLs. We also created a simple app that displays a list of books. This is just the beginning of your Django journey. With practice and experience, you can build complex and scalable websites using Django. Remember to always follow best practices and keep your code organized and readable.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *