Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It was designed to help developers take applications from concept to completion as quickly as possible. In this section, we will cover the basics of Django, including setting up a Django project, understanding its structure, and creating a simple web application.
Key Concepts
- Django Framework Overview
- Setting Up Django
- Django Project Structure
- Creating a Simple Django Application
- Django Models, Views, and Templates (MVT)
- Admin Interface
- URL Routing
- Django Framework Overview
Django is built on the principle of "Don't Repeat Yourself" (DRY), which means it tries to minimize repetition of code. It follows the Model-View-Template (MVT) architectural pattern.
- Model: Defines the data structure.
- View: Controls what data is presented and how it is displayed.
- Template: Defines the HTML structure of the web pages.
- Setting Up Django
Prerequisites
- Python installed on your system.
- Basic understanding of Python programming.
Installation
To install Django, use the following command:
Verify the installation by checking the Django version:
Creating a Django Project
To create a new Django project, use the django-admin
command:
Navigate to the project directory:
- Django Project Structure
A Django project consists of several files and directories. Here is an overview of the structure:
- manage.py: A command-line utility that lets you interact with the Django project.
- settings.py: Configuration file for the project.
- urls.py: URL declarations for the project.
- wsgi.py: Entry-point for WSGI-compatible web servers to serve your project.
- Creating a Simple Django Application
Creating an App
Inside your project directory, create a new app:
This will create a new directory myapp
with the following structure:
Registering the App
Add the new app to the INSTALLED_APPS
list in settings.py
:
- Django Models, Views, and Templates (MVT)
Models
Define your data models in models.py
:
from django.db import models class Item(models.Model): name = models.CharField(max_length=100) description = models.TextField() def __str__(self): return self.name
Run the following commands to create the database tables:
Views
Define your views in views.py
:
from django.shortcuts import render from .models import Item def item_list(request): items = Item.objects.all() return render(request, 'item_list.html', {'items': items})
Templates
Create a directory named templates
inside your app directory and add an HTML file item_list.html
:
<!DOCTYPE html> <html> <head> <title>Item List</title> </head> <body> <h1>Items</h1> <ul> {% for item in items %} <li>{{ item.name }}: {{ item.description }}</li> {% endfor %} </ul> </body> </html>
URL Routing
Define the URL pattern for your view in urls.py
:
from django.urls import path from . import views urlpatterns = [ path('items/', views.item_list, name='item_list'), ]
Include the app's URL patterns in the project's urls.py
:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('', include('myapp.urls')), ]
- Admin Interface
Django provides a built-in admin interface to manage your models. Register your models in admin.py
:
Run the development server:
Access the admin interface at http://127.0.0.1:8000/admin/
and log in with the superuser credentials.
- URL Routing
Django uses URL routing to map URLs to views. The urls.py
file in your project and app directories define these mappings.
Example
from django.urls import path from . import views urlpatterns = [ path('items/', views.item_list, name='item_list'), ]
Conclusion
In this section, we covered the basics of Django, including setting up a project, understanding its structure, and creating a simple web application. We also explored the MVT pattern, URL routing, and the admin interface. In the next section, we will dive deeper into building more complex web applications using Django.
Python Programming Course
Module 1: Introduction to Python
- Introduction to Python
- Setting Up the Development Environment
- Python Syntax and Basic Data Types
- Variables and Constants
- Basic Input and Output
Module 2: Control Structures
Module 3: Functions and Modules
- Defining Functions
- Function Arguments
- Lambda Functions
- Modules and Packages
- Standard Library Overview
Module 4: Data Structures
Module 5: Object-Oriented Programming
Module 6: File Handling
Module 7: Error Handling and Exceptions
Module 8: Advanced Topics
- Decorators
- Generators
- Context Managers
- Concurrency: Threads and Processes
- Asyncio for Asynchronous Programming
Module 9: Testing and Debugging
- Introduction to Testing
- Unit Testing with unittest
- Test-Driven Development
- Debugging Techniques
- Using pdb for Debugging
Module 10: Web Development with Python
- Introduction to Web Development
- Flask Framework Basics
- Building REST APIs with Flask
- Introduction to Django
- Building Web Applications with Django
Module 11: Data Science with Python
- Introduction to Data Science
- NumPy for Numerical Computing
- Pandas for Data Manipulation
- Matplotlib for Data Visualization
- Introduction to Machine Learning with scikit-learn