Creating a Django registration form First, define a registration URL in the urls.py of the users app: from django.urls import path from . import views urlpatterns = [ path(‘login/’, views.sign_in, name=’login’), path(‘logout/’, views.sign_out, name=’logout’), path(‘register/’, views.sign_up, name=’register’), ] Code language: Python (python) Second, define a RegisterForm class in the forms.py of the users.py file: from django import forms from django.contrib.auth.modelsContinue 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

Introduction to Django models In Django, a model is a subclass of the django.db.models.Model class. A model contains one or more fields and methods that manipulate the fields. Essentially, a Django model maps to a single table in the database in which each field of the model represents a column in theContinue Reading

Introduction to the Django templates In the previous tutorial, you learned how to return a HttpResponse with a h1 tag from a view. To return a full HTML page, you need to use a template. A template is a file that contains the static and dynamic parts of a webpage. To generate the dynamicContinue Reading