Django Tutorial
×


Django Tutorial

465

Introduction

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. It follows the "batteries-included" philosophy, meaning it comes with a lot of built-in features like an admin panel, authentication system, and ORM (Object-Relational Mapping). This Django Tutorial will help you get started with the basics and advance toward building robust web applications.

Why Use Django?

Django promotes clean, pragmatic design and is ideal for developers who want to focus on writing app logic rather than reinventing common components. Some key advantages include:

  • Secure by default (protects against common attacks)
  • Comes with an admin interface for managing models
  • Scalable and flexible for both small and large applications
  • Built-in ORM to interact with databases efficiently

Installing Django

Before working with Django, make sure Python is installed on your system. It's a good practice to use a virtual environment.

pip install django

To verify installation:

django-admin --version

Creating Your First Django Project

Use the following command to start a new project:

django-admin startproject mysite

This command creates a new Django project named mysite. You can run the development server by navigating to the project directory and running:

python manage.py runserver

Now open http://127.0.0.1:8000/ in your browser to see the default Django welcome page.

Understanding the Project Structure

After creating a project, you'll see several files:

  • manage.py – utility for administrative tasks
  • settings.py – configuration for your project
  • urls.py – URL declarations
  • wsgi.py – entry point for WSGI-compatible web servers

Creating a Django App

Django follows a project-app structure. To add functionality, you create apps within your project.

python manage.py startapp blog

This will generate a new folder named blog containing files like views.py, models.py, and admin.py.

Working with Views and URLs

Views are Python functions or classes that return responses to user requests. Let’s define a simple view in views.py:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django!")

Next, map this view to a URL in urls.py of your app or project.

from django.urls import path
from . import views

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

Creating Templates

Django uses templates to render HTML. Create a templates folder inside your app and add HTML files:

<!-- home.html -->
<html>
  <body>
    <h1>Welcome to Django!</h1>
  </body>
</html>

Then, render the template from the view using render().

from django.shortcuts import render

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

Database and Models

Django comes with an integrated ORM system. Define your models in models.py:

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()

Run the following commands to create and apply migrations:

python manage.py makemigrations
python manage.py migrate

Admin Interface

Django's admin panel is a powerful feature for managing app data. To enable it, register your model in admin.py:

from .models import Post
from django.contrib import admin

admin.site.register(Post)

Create a superuser account:

python manage.py createsuperuser

After logging into the admin panel at /admin, you can add and manage data easily.

Conclusion

Django is a powerful and robust framework that simplifies web development. Whether you're building a blog, e-commerce store, or a large-scale enterprise system, Django provides all the tools you need. This Django tutorial offers a great starting point for creating professional web applications with Python.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat