Introduction to Django
Django is a high-level, open-source Python web framework that enables rapid development of secure and maintainable websites. It follows the Model-View-Template (MVT) architectural pattern and encourages the use of reusable code by providing a rich set of tools and libraries.
Django App Structure
A Django project consists of multiple apps, each representing a logical part of the overall application. A typical Django app has the following components:
- Models: Define the data structure and logic of the application.
- Views: Handle the logic for processing user requests and returning responses.
- Templates: Define the structure and layout of HTML pages.
Models, Views, and Templates
Models in Django represent database tables and define the fields and behavior of the data. Views process user requests, interact with models, and return responses (usually rendered templates). Templates are HTML files with placeholders for dynamic content, which are filled in by views.
Example of creating a simple Django model, view, and template:
1. Create a model in ‘models.py‘:
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=100)
completed = models.BooleanField(default=False)
2. Create a view in ‘views.py‘:
from django.shortcuts import render
from .models import Task
def task_list(request):
tasks = Task.objects.all()
return render(request, 'tasks/task_list.html', {'tasks': tasks})
3. Create a template ‘task_list.html‘:
{% for task in tasks %}
<p>{{ task.title }} - {% if task.completed %}Completed{% else %}Not completed{% endif %}</p>
{% endfor %}
Forms and Authentication
Django provides built-in support for handling forms and authentication. Django forms enable developers to handle user input, validation, and submission, while authentication allows for user registration, login, and access control.
Hands-on Exercises
- Set up a new Django project and create a simple blog application with models, views, and templates.
- Implement user registration and login using Django’s built-in authentication system.
- Create a to-do list app in Django, with the ability to add, edit, and delete tasks.
Real-World Example
Django is used by many large-scale websites and applications, including Instagram, Disqus, and Mozilla.
Additional Resources
- Django documentation: https://docs.djangoproject.com/en/3.2/
- Django for beginners: https://djangoforbeginners.com/
Review Questions
- What is Django, and what types of applications can you build with it?
- Describe the role of models, views, and templates in a Django application.
- How can you handle user input and authentication in Django?
Summary and Next Steps
In this lesson, you have learned about:
- The Django web framework and its components
- Creating Django models, views, and templates
- Handling forms and authentication in Django
In the next lesson, you will learn about big data processing with Python.
Remember to review the material periodically and practice your skills to reinforce your learning. Good luck on your journey to becoming a Python developer!



