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. View

Project Todo Views

you're building a to-do list application using Django and you want to customize some views for user authentication and task management.

Views of Todo App

Below is the entire views of the todo app:

from django.shortcuts import render, redirect
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView, FormView
from django.urls import reverse_lazy

from django.contrib.auth.views import LoginView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login

# Imports for Reordering Feature
from django.views import View
from django.shortcuts import redirect
from django.db import transaction

from .models import Task
from .forms import PositionForm

class CustomLoginView(LoginView):
    template_name = 'base/login.html'
    fields = '__all__'
    redirect_authenticated_user = True

    def get_success_url(self):
        return reverse_lazy('tasks')


class RegisterPage(FormView):
    template_name = 'base/register.html'
    form_class = UserCreationForm
    redirect_authenticated_user = True
    success_url = reverse_lazy('tasks')

    def form_valid(self, form):
        user = form.save()
        if user is not None:
            login(self.request, user)
        return super(RegisterPage, self).form_valid(form)

    def get(self, *args, **kwargs):
        if self.request.user.is_authenticated:
            return redirect('tasks')
        return super(RegisterPage, self).get(*args, **kwargs)


class TaskList(LoginRequiredMixin, ListView):
    model = Task
    context_object_name = 'tasks'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['tasks'] = context['tasks'].filter(user=self.request.user)
        context['count'] = context['tasks'].filter(complete=False).count()

        search_input = self.request.GET.get('search-area') or ''
        if search_input:
            context['tasks'] = context['tasks'].filter(
                title__contains=search_input)

        context['search_input'] = search_input

        return context



class TaskCreate(LoginRequiredMixin, CreateView):
    model = Task
    fields = ['title', 'description', 'complete']
    success_url = reverse_lazy('tasks')

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(TaskCreate, self).form_valid(form)


class TaskUpdate(LoginRequiredMixin, UpdateView):
    model = Task
    fields = ['title', 'description', 'complete']
    success_url = reverse_lazy('tasks')


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)

This Django project includes views for handling tasks such as user authentication, task creation, updating, and deletion. The application features a custom login view (CustomLoginView) and a registration view (RegisterPage). It utilizes Django's generic class-based views like ListView, DetailView, CreateView, UpdateView, and DeleteView to manage tasks. The tasks are associated with users, ensuring that each user only sees their tasks.

Key components:

  1. CustomLoginView: Extends Django's LoginView for user authentication. It redirects authenticated users to the 'tasks' page upon successful login.

  2. RegisterPage: Extends FormView to handle user registration using UserCreationForm. After successful registration, users are automatically logged in and redirected to the 'tasks' page.

  3. TaskList: Extends ListView and requires user login. Displays a list of tasks associated with the logged-in user. Supports filtering tasks by a search input.

  4. TaskDetail: Extends DetailView and requires user login. Displays details for a specific task.

  5. TaskCreate: Extends CreateView and requires user login. Allows users to create new tasks.

  6. TaskUpdate: Extends UpdateView and requires user login. Allows users to update existing tasks.

  7. DeleteView: Extends DeleteView and requires user login. Allows users to delete tasks, and the get_queryset method ensures that users can only delete their own tasks.

  8. TaskReorder: Extends View to handle reordering tasks. Uses a custom form (PositionForm) to receive the new order of tasks and updates the task order for the current user within a transaction.

Additionally, the application uses a Task model to represent tasks, and there is a PositionForm for handling the task reordering feature. The project also includes templates for login, registration, task list, task detail, and task creation.

Note: The get_queryset method in the DeleteView class has a typo in the method name. It should be get_object instead of get_queryset.

PreviousTypes of Views in DjangoNextDjango Templates

Last updated 1 year ago