Create a Django DeleteView class The Django DeleteView class allows you to define a class-based view that displays a confirmation page and deletes an existing object. If the HTTP request method is GET, the DeleteView view will display the confirmation page. However, if the request is POST, the DeleteView view will delete the object. To use the DeleteView class, you defineContinue Reading

Defining the UpdateView class The UpdateView class allows you to create a class-based view that: The form is generated automatically from the object’s model unless you explicitly specify a form class. To demonstrate the Django UpdateView class, we’ll create a view that edits a task of the Todo App. To do that we modify the views.py of the todo appContinue Reading

Defining the CreateView class The CreateView class allows you to create a class-based view that displays a form for creating an object, redisplaying the form with validation errors, and saving the object into the database. To use the CreateView class, you define a class that inherits from it and add some attributes and methods. For example,Continue Reading

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

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