Simple Web Development with Python Django
Python is one of the most popular and versatile programming languages, attracting developers from all backgrounds. One of the most invaluable tools available for Python developers is the Django web framework, which facilitates quick and easy web development with Python. In this article, we will introduce the fundamentals of Django and show you how to build a basic web application.
What is Django?
Django is a Python web framework that streamlines web development by providing a set of tools and libraries that help developers build scalable web applications. Its main selling point is that it emphasizes reusable code, which saves development time and effort. Django provides a robust and extensive infrastructure for web development, covering everything from handling HTTP requests and responses to handling databases and user authentication.
One of the key features of Django is its Model-View-Controller (MVC) architecture. It is designed to help separate frontend presentation from backend logic while enforcing the adherence of naming conventions.
Key Features of Django
There are several key features that make Django stand out as an excellent framework for web development in Python.
Object-Relational Mapping (ORM)
Django provides a built-in Object-Relational Mapping (ORM) system that facilitates communication between Python code and databases such as MySQL or PostgreSQL. The ORM handles complex database operations like inserting records, updating records, and also retrieving records from the database. An ORM is beneficial because it simplifies the code required to manage the database, making the code more readable and reusable.
Routing System
Django has a powerful routing system that is responsible for mapping URLs to views that can handle HTTP requests. The routing system removes the need to write a lot of boilerplate code for regular tasks so that developers can focus on writing more useful application code.
Authentication System
Authentication is an essential feature of web development, and Django provides a built-in authentication system that makes it easy to manage users and user roles. The Django authentication system also provides ways to handle password storage and resetting, session management, and permissions, which simplifies user management.
Template System
The Django framework provides a template system that enables developers to design and render HTML templates easily. The template system comes with built-in tags and filters that can be used to manage context data and provide an easy way for you to reuse code.
Creating a Django Web Application
Creating a Django web application is a simple process, after installing Django with pip, you can run the following command:
$django-admin startproject project_name
Let’s breakdown what this command does:
– `django-admin` is a command-line tool that allows you to automate the creation of Django project structure.
– `startproject` is a command that tells ‘django-admin’ to create a new project.
– `project_name` is the name of your project.
After running this command, Django will create a directory structure that should look like this:
“`
project_name/
manage.py
project_name/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
“`
We can now run the project using the following command:
$python manage.py runserver
This command will start a local development server that will automatically reload your project whenever you make a change to your code:
Starting development server at http://127.0.0.1:8000/
Create a Django App
A Django project may consist of multiple apps that function independently, but together create a complete project. To create an app in Django, we will run the following command:
$python manage.py startapp app_name
This command creates a new directory structure in our project that should now look like this:
“`
project_name/
manage.py
project_name/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
app_name/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
“`
Create a View in Django
A view in Django is a function that takes an HTTP request as an argument and returns an HTTP response. It’s responsible for handling the request from the routing system and returning a proper HTTP response.
Here’s a simple view example:
“`
from django.http import HttpResponse
def home(request):
return HttpResponse(‘Hello, World’)
“`
The view above returns a basic HTTP response containing the string ‘Hello, World’.
Create a URL in Django
A URL in Django maps to a view that will handle the HTTP request. In Django, we use a file called urls.py to specify URL patterns.
Here’s an example URL pattern:
“`
from django.urls import path
from . import views
urlpatterns = [
path(”, views.home, name=’home’),
]
“`
This route maps the root URL `/` to the view `home()`. In this way, Django maps URLs to views, making it easy to create URLs for our web applications.
Conclusion
In this article, we have introduced the fundamentals of Django and shown you how to build a basic web application using the framework. Django’s powerful tools and libraries make web development with Python more efficient and less cumbersome. With the help of Django, you can build scalable web applications using Python with ease.