User Authentication Based Django App
  • Python Django
  • Basic Python Virtual Environment Setup
  • Project Set Up
  • Model-View-Templates Implementation
  • Model & Django Admin Area
  • View
    • Types of Views in Django
    • Project Todo Views
  • Django Templates
  • Todo App
    • DEMO: Workflow of Todo App
    • Base Template for Todo App
    • Registration Module
    • Login Module
    • Linking Login and Registration page
    • Task List User Authentication
    • Task Reordering
    • Logout Module
    • Task Create Module
    • Task Update
    • Task Delete
Powered by GitBook
On this page
  • 1. Create template for Task Delete module.
  • 2. Create view function for Task Delete module
  • 3. Mapping URLs to Task Create view.
  • 4. Update the task_list.html
  • add href value to line no. 51 in task_list.html
  • 5. Run the development server to check the functionality:
  1. Todo App

Task Delete

Steps to Develop a Task Delete module.

1. Create template for Task Delete module.

templates/todo/task_confirm_delete.html

{% extends 'todo/main.html' %}
{% block content %}

<div class="header-bar">
    <a href="{% url 'tasks' %}">&#8592; Go Back</a>
</div>

<div class="card-body">
    <form method="POST">
        {% csrf_token %}
        <p>Are your sure you want to delete this task? <b>"{{task}}"</b> </p>
        <input class="button" type="submit" value="Delete" />
    </form>
</div>



{% endblock content %}

2. Create view function for Task Delete module

  1. Import Statements:

    from django.contrib.auth.mixins import LoginRequiredMixin
    from django.urls import reverse_lazy
    from django.views.generic.edit import DeleteView
    from .models import Task  # Import your Task model

    These statements import the necessary classes for creating a Task Create view and applying the LoginRequiredMixin.

  2. Class Definition:

class DeleteView(LoginRequiredMixin, DeleteView):
    model = Task
    context_object_name = 'task'
    success_url = reverse_lazy('tasks')
    def get_queryset(self):
        owner = self.request.user
        return self.model.objects.filter(user=owner)

Django class-based Delete view for deleting a Task model object. This view inherits from LoginRequiredMixin and DeleteView. The get_queryset method is overridden to ensure that only the tasks owned by the currently logged-in user are available for deletion.

Here's a breakdown of the code:

  1. LoginRequiredMixin: This ensures that the user must be logged in to access the view.

  2. DeleteView: This is a generic class-based view in Django for deleting an object. It provides a default implementation for deleting an object and rendering a confirmation page.

  3. model = Task: Specifies the model that this view is associated with (in this case, the Task model).

  4. context_object_name = 'task': Sets the name of the variable that will be used in the template context to represent the object being deleted.

  5. success_url = reverse_lazy('tasks'): Defines the URL to redirect to after a successful deletion. In this case, it uses reverse_lazy to reverse-resolve the URL named 'tasks'. Make sure you have a URL pattern named 'tasks' defined in your Django project's URL configuration.

  6. get_queryset: Overrides the default implementation to filter the queryset based on the current user. It ensures that only tasks belonging to the logged-in user are retrieved.

This code ensures that a deleted task is associated with the currently logged-in user when it is created.

3. Mapping URLs to Task Create view.

Make sure you have the necessary URLs and other configurations in place to use this Task Create view. For example, in your urls.py file, you might have something like:

from django.urls import path

from .views import DeleteView

urlpatterns = [
    path('task-delete/<int:pk>/', DeleteView.as_view(), name='task-delete'),
    # Add other URL patterns as needed
]

4. Update the task_list.html

add href value to line no. 51 in task_list.html

href="{% url 'task-delete' task.id %}"

5. Run the development server to check the functionality:

Start the development server if it's not running already:

python manage.py runserver

Access the interface by visiting http://localhost:8000/ in your web browser.

PreviousTask Update

Last updated 1 year ago