Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It is often referred to as “the framework for perfectionists with deadlines” due to its emphasis on reusability, less code, and the principle of “don’t repeat yourself.” This article will guide you through your journey from zero knowledge to becoming proficient in building web applications using Django.
Understanding Django
Django was created in 2003 by web programmers Adrian Holovaty and Simon Willison while they were working at a newspaper called The World Company. The framework was designed to help developers take applications from concept to completion as quickly as possible.
The core philosophy of Django encompasses the following principles:
- DRY (Don’t Repeat Yourself): This principle aims to eliminate redundancy by allowing developers to reuse existing code and components.
- Loosely Coupled: All layers in the Django stack are independent, allowing developers to replace or modify components independently of others.
- Fast Development: Built-in features help developers complete projects faster without compromising on functionality.
- Security: Django developers take security seriously, helping to protect applications from common threats.
Setting Up Your Development Environment
To start your journey with Django, you need to set up your development environment. Follow the quick steps to get started:
Install Python
Django is a Python framework; thus, you need to ensure Python is installed on your machine. You can download the latest version from the official Python website.
Install Django
pip install django
This command will download and install Django on your machine, making it ready for development.
Creating Your First Django Project
Once the installation is complete, you can create a new Django project using the following command:
django-admin startproject myproject
Navigate into your project directory to discover the typical Django project structure. The key components include:
manage.py:A command-line utility that lets you interact with the Django project.settings.py:The configuration file for your project.urls.py:Contains URL patterns and routes for the Django application.
Build Your First Application
With your project set up, it’s time to create your first application. An application in Django is a web application that does something, such as a blog or an online store.
Create a New App
python manage.py startapp myapp
This command creates a new application within your project. The myapp directory contains fundamental files such as:
views.py:Handles the logic and returns a response to the user.models.py:Defines the data structure.admin.py:Registers models to the Django admin site.
Register the App
You must register the new app in your project’s settings. Add 'myapp', to the INSTALLED_APPS list in settings.py.
Define a Simple View
Open views.py and define a simple view that returns a “Hello, World!” message:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, World!")
Set Up a URL Pattern
Create a urls.py file in your app directory and set up a URL pattern for your view:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Include this app’s URL pattern 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')),
]
Working with Models
Django models define the structure of your database. They are essential elements for any Django web application.
Defining a Model
Open models.py and create a new model:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
def __str__(self):
return self.title
Migrate the Model
Run the following commands to create the database table:
python manage.py makemigrations
python manage.py migrate
These commands create a migration file and apply the changes to the database.
Register the Model in Admin
Open admin.py and register your model:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Templates in Django
Django templates allow you to create dynamic HTML content. They enable you to separate the presentation layer from the application logic.
Create a Template
Create a directory called templates in your app directory, and inside it, create an HTML file such as home.html. Your template might look like this:
Home


0 Comments