Defining a DetailView The Django DetailView allows you to define a class-based view that displays the details of an object. To use the DetailView class, you define a class that inherits from the DetailView class. For example, the following defines the TaskDetail class-based view that displays the detail of a task of the Todo app: from django.shortcuts import renderContinue Reading

Introduction to the class-based views In the previous tutorials, you have learned how to build a blog application using function-based views. The function-based views are simple but flexible. In the earlier versions, Django only supported function-based views. Later, Django added support for class-based views that allow you to define viewsContinue Reading

Creating a virtual environment Run the following python command to create a virtual environment using the built-in venv module: python -m venv venvCode language: plaintext (plaintext) And activate the venv virtual environment using the following command: venv\scripts\activateCode language: plaintext (plaintext) If you use macOS and Linux, you can use the following python3 command to create a new virtualContinue Reading

Create a new application First, create a new application called users by executing the startapp command: The project directory will look like this: ├── blog ├── db.sqlite3 ├── manage.py ├── mysite ├── static ├── templates └── usersCode language: plaintext (plaintext) Second, register the users application in the installed apps of the settings.py of the project: INSTALLED_APPS = [ ‘django.contrib.admin’, ‘django.contrib.auth’,Continue Reading

We’ll create a form that deletes a post by its id. Creating an URL pattern Add an URL pattern to the pattern list in the urls.py of the blog application: from django.urls import path from . import views urlpatterns = [ path(”, views.home, name=’posts’), path(‘post/create’, views.create_post, name=’post-create’), path(‘post/edit/<int:id>/’, views.edit_post, name=’post-edit’), path(‘post/delete/<int:id>/’, views.delete_post, name=’post-delete’), path(‘about/’,Continue Reading

We’ll create a Django Edit Form that updates a blog post and saves the changes into the database. Create an URL pattern First, create a URL pattern for editing a post: from django.urls import path from . import views urlpatterns = [ path(”, views.home, name=’posts’), path(‘post/create’, views.create_post, name=’post-create’), path(‘post/edit/<int:id>/’, views.edit_post,Continue Reading

Introduction to the Django Flash Messages A flash message is a one-time notification message. To display the flash message in Django, you use the messages from django.contrib module: from django.contrib import messagesCode language: Python (python) The messages have some useful functions for displaying information, warning, and error messages: All of these functionsContinue Reading

Django admin is good enough for admin to manage the contents based on the models. However, when you want the users of the website to manage their content, you need to create separate forms for them. Introduction to the Django Form Handling forms involves very complex logic: Django forms simplifyContinue Reading

Introduction to the Django admin page When you create a new project using the startproject command, Django automatically generates the admin page for managing models including creating, reading, updating, and deleting which is often known as CRUD. To access the admin page, you navigate to the URL http://127.0.0.1/admin/. It’ll open the login page:Continue Reading

Introduction to Django migration commands When working with Django, you don’t need to write SQL to create new tables or make changes to existing tables. Instead, you use Django migrations. Django migrations allow you to propagate the changes that you make to the models to the database via the command line. DjangoContinue Reading