Python and Django have become favorites of many developers due to their multipurpose nature. Python is a general-purpose programming language that is easy to learn, read and maintain whereas Django is a high-level Python web framework that encourages rapid development and pragmatic, clean design.
This tutorial will take you through the process of building a web application with Python and Django. You will learn how to use Django’s built-in features to create a robust and secure web application. So, let’s get started.
Setting up the environment
Before we begin, make sure you have Python and Django installed on your system. You can install Python by downloading the latest version from the official website, and Django can be installed through pip, the package manager of Python.
After installing Python and Django, create a new project in Django using the following command:
django-admin startproject projectname
This will create a new Django project with the name “projectname.” Now, move into the project directory using the command:
cd projectname
Creating an app
In Django, an app is a self-contained module that represents a single unit of functionality. To create a new app, run:
python manage.py startapp appname
This command will create a new directory inside your project directory with the name “appname.” This directory will contain all the files required for the app. Now, open the settings.py file in your project directory and add your app to the INSTALLED_APPS list:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'appname', ]
Models and Database
In Django, a model is a Python class that represents a database table. You need to define models for your app in models.py file. For example, let’s create a model for storing blog posts in our app.
from django.db import models class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() pub_date = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title
Now, you need to run the following command to create database tables based on your defined models:
python manage.py makemigrations python manage.py migrate
This will create the necessary database tables for your app.
URLs and Views
To get started with creating views and URLs, open urls.py file in your app’s directory and add the following code:
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
Now, create a view function in views.py file to handle the above-defined URL:
from django.shortcuts import render from .models import Post def index(request): posts = Post.objects.all() return render(request, 'index.html', {'posts': posts})
This view function will retrieve all blog posts from the database and pass them to the template, which we will create later.
Templates and Static Files
In Django, templates are used to render dynamic HTML pages, and static files are used for styles, images, and other assets. Create a new directory called “templates” and create a new HTML file inside it called “index.html.”
Blog Posts Blog Posts
{% for post in posts %}{{ post.title }}
{{ post.content }}
{{ post.pub_date }}
{% endfor %}
This template will display all the blog posts retrieved from the database.
To add static files, create a directory called “static” and add your CSS, JavaScript, and images files.
In this tutorial, we learned the basics of building a web application with Python and Django. We created a new project, created an app, defined models and database, and created a URL and view. We also learned how to use templates and static files to render dynamic HTML pages.
Django provides an extensive set of tools and features that make building web applications easier and faster.
Want to learn more about Python, checkout the Python Official Documentation for detail.