Python Django Basics

Django is a high-level web framework for Python that allows developers to build web applications quickly and with ease. It follows the “Don’t Repeat Yourself” (DRY) principle, making it highly efficient for creating scalable and maintainable web applications. In this tutorial, we’ll explore the basics of Django, including how to set up a Django project, create models, views, and templates, and work with databases.

1. Introduction to Django

Django was designed to help developers build web applications faster with minimal code. It is packed with built-in features, such as authentication, URL routing, and an admin panel, which makes it one of the most popular Python web frameworks.

2. Installing Django

Before you can start using Django, you need to install it. You can install Django using pip, the Python package manager.

pip install django

Try It Now

3. Creating a Django Project

Once Django is installed, you can create a new project using the django-admin command. Here’s how to start a new Django project:

django-admin startproject myproject
cd myproject

Try It Now

This will create a new directory called myproject with the basic structure of a Django project. You can run the development server with the following command:

python manage.py runserver

Try It Now

Your Django application will be running at http://127.0.0.1:8000/ by default. You should see the Django welcome page when you visit the URL in your browser.

4. Django App Structure

In Django, a project is made up of one or more apps. An app is a component that handles a specific part of your application. To create an app in Django, run the following command:

python manage.py startapp myapp

Try It Now

This will create a directory called myapp containing the files needed for your Django app.

5. Creating Models in Django

Models in Django are used to define the structure of your database. Each model represents a table in the database, and each attribute of the model represents a column in that table. To create a model, open the models.py file inside your app directory.

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    published_date = models.DateField()
    isbn_number = models.CharField(max_length=13)

    def __str__(self):
        return self.title

Try It Now

In the above example, we defined a Book model with four fields: title, author, published_date, and isbn_number. To create the corresponding table in the database, run the following commands:

python manage.py makemigrations
python manage.py migrate

Try It Now

6. Creating Views in Django

In Django, views are functions that receive web requests and return web responses. Views are defined in the views.py file within your app directory. Here’s how to define a basic view that renders a template:

from django.shortcuts import render

def home(request):
    return render(request, 'home.html')

Try It Now

The home view renders a template called home.html. To link this view to a URL, you need to define a URL pattern in the urls.py file.

6.1 Mapping Views to URLs

In Django, URL patterns are defined in the urls.py file. Here’s an example of how to link the home view to the home page:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

Try It Now

7. Using Templates in Django

In Django, templates are used to render HTML files with dynamic content. Templates are stored in the templates directory inside your app or project directory.

7.1 Creating a Template

Create a new file called home.html inside the templates directory:





    
    
    Home Page


    

Welcome to the Home Page

Try It Now

This template will be rendered when the user visits the home page of your site.

8. Django Admin Panel

One of the most powerful features of Django is the built-in admin panel, which allows you to manage your app’s data through a web interface. To enable the admin panel, you need to register your models in the admin.py file.

from django.contrib import admin
from .models import Book

admin.site.register(Book)

Try It Now

After registering your model, create a superuser by running the following command:

python manage.py createsuperuser

Try It Now

Once the superuser is created, you can log in to the admin panel by visiting http://127.0.0.1:8000/admin and managing your data from the web interface.

Conclusion

In this tutorial, you learned the basics of Django, including how to set up a Django project, create models, views, and templates, and work with the admin panel. Django is a powerful framework that helps developers build web applications quickly and efficiently. With its built-in features, such as authentication, form handling, and the admin panel.