Hello everyone and sorry in advance for my broken English.
I created a django project with multiple apps all gathered on a central page/app called core
All my apps work except the Taskboard app: A small module like a notification badge which displays the tasks assigned to a staff and which, once completed, allows them to be validated via the button of the same name)
Preview :
My problem is that when I check one of the boxes and press the validate button… nothing happens.
Message on my django console : “POST /taskboard/validate_tasks/ HTTP/1.1” 302 0
Here is my code, I apologize in advance it really became a mess after looking for solutions everywhere :
App Taskboard :
# models.py
from django.db import models
from django.contrib.auth.models import User
class Task(models.Model):
# Relais avec l'objet User
user = models.ForeignKey(User, on_delete=models.CASCADE)
# Nom de l'utilisateur qui a créé la tâche
nom_personnel = models.CharField(max_length=255, blank=True, null=True, default='')
creation_date = models.DateTimeField(auto_now_add=True)
due_date = models.DateTimeField()
statement = models.TextField()
completed = models.BooleanField(default=False)
def __str__(self):
return f"Tâche pour {self.user.username}"
class ArchiveTask(models.Model):
nom_personnel = models.CharField(max_length=255)
date_creation = models.DateTimeField(auto_now_add=True)
date_fin_demandee = models.DateTimeField()
enonce = models.TextField()
def __str__(self):
return f"{self.nom_personnel} - {self.date_creation}"
# taskboard/views.py
from django.shortcuts import render, redirect
from .models import Task, ArchiveTask
from django.contrib.auth.decorators import login_required
from core.views import homepage # Mettez à jour l'import
from django.urls import reverse
@login_required
def task_list(request):
user_tasks = Task.objects.filter(user=request.user, completed=False)
task_count = user_tasks.count()
return {'user_tasks': user_tasks, 'task_count': task_count}
@login_required
def validate_tasks(request):
if request.method == 'POST':
# Get the list of task IDs from the POST request
task_ids = request.POST.getlist('task_ids')
# Iterate through the task IDs and archive/delete the tasks
for task_id in task_ids:
task = Task.objects.get(pk=task_id)
archive_task = ArchiveTask(
nom_personnel=task.user.username,
date_creation=task.creation_date,
date_fin_demandee=task.due_date,
enonce=task.statement
)
archive_task.save()
task.delete()
# Get the URL corresponding to the homepage view
homepage_url = reverse('core:homepage')
# Redirect to the homepage
return redirect(homepage_url)
# taskboard/urls.py
from django.urls import path
from .views import task_list, validate_tasks
app_name = 'taskboard'
urlpatterns = [
path('task_list/', task_list, name='task_list'),
path('validate_tasks/', validate_tasks, name='validate_tasks'),
]
<!-- taskboard/task_list.html -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/taskboard.css' %}">
<div id='taskboardcss'>
<!-- Ajout de l'icône avec un badge -->
<a href="#" id="icon-link">
<span id="task-badge">{{ task_count }}</span>
<img id="icon-user" src="{% static 'image/utilisateur.png' %}" alt="Task Icon">
</a>
<!-- Fenêtre déroulante -->
<div id="dropdown-content">
<ul>
{% for task in user_tasks %}
<li>
<div>
<strong>Crée le:</strong> {{ task.creation_date }}
</div>
<div>
<strong>A faire le:</strong> {{ task.due_date }}
</div>
<div>
<strong>Tache :</strong> {{ task.statement }}
</div>
<div>
<strong>OK :</strong> <input type="checkbox" name="task_ids" value="{{ task.id }}">
</div>
</li>
{% endfor %}
</ul>
<form method="post" action="{% url 'taskboard:validate_tasks' %}">
{% csrf_token %}
<input type="submit" value="Valider">
</form>
</div>
<!-- Bouton pour ouvrir la fenêtre déroulante -->
<script>
document.addEventListener("DOMContentLoaded", function () {
var iconLink = document.getElementById("icon-user");
var dropdownContent = document.getElementById("dropdown-content");
iconLink.addEventListener("click", function (event) {
event.preventDefault();
// Toggle display
dropdownContent.style.display = (dropdownContent.style.display === 'none' || dropdownContent.style.display === '') ? 'block' : 'none';
});
window.addEventListener("click", function (event) {
// Vérifier que l'élément cliqué n'est pas l'icône
if (event.target !== iconLink && event.target !== dropdownContent && !dropdownContent.contains(event.target)) {
dropdownContent.style.display = "none";
}
});
});
</script>
</div>
App Core :
# core/views.py
from django.shortcuts import render, redirect
from menulateral.models import MenuItem
from menuvertical.models import MenuItemVertical
from alerte.models import Appli, Alerte
from alerte.forms import AlerteForm
from taskboard.models import Task, ArchiveTask
from filactu.views import ActivityListView
from django.contrib.auth.decorators import login_required
@login_required
def homepage(request):
vertical_menu_items = MenuItemVertical.objects.all()
lateral_menu_items = MenuItem.objects.all()
liste_alertes_items = Alerte.objects.all()
liste_applications_items = Appli.objects.all()
form = AlerteForm(request.POST)
if request.method == 'POST':
form = AlerteForm(request.POST)
if form.is_valid():
form.save()
# Filtrer les tâches par utilisateur
liste_task_items = Task.objects.filter(user=request.user)
liste_taskarchive_items = ArchiveTask.objects.all()
# Utilisez la méthode get_context_data pour obtenir le contexte
activities_context = ActivityListView().get_context_data()
return render(request, 'core/homepage.html', {
'vertical_menu_items': vertical_menu_items,
'lateral_menu_items': lateral_menu_items,
'liste_alertes_items': liste_alertes_items,
'liste_applications_items': liste_applications_items,
'liste_task_items': liste_task_items,
'liste_taskarchive_items': liste_taskarchive_items,
'form': form,
**activities_context # Utilisez les données de contexte de l'app filactu
})
from django.urls import path
from . import views
from taskboard.views import validate_tasks
app_name = 'core'
urlpatterns = [
path('', views.homepage, name='homepage'),
path('taskboard/validate_tasks/', validate_tasks, name='taskboard_validate_tasks'), # Utiliser l'espace de noms
]
<!DOCTYPE html>
<html lang="fr">
{% csrf_token %}
{% load static %}
<head>
<meta charset="UTF-8">
<link rel="favico icon" href="{% static 'image/logocentralisation.png' %}"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centralis@tion 3.0</title>
<link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<header>
{% include "menuvertical/menu_verticale.html" %}
</header>
<aside>
{% include "menulateral/menu_template.html" %}
</aside>
{% include "filactu/filactu.html" %}
{% include "taskboard/task_list.html" with user_tasks=liste_task_items task_count=liste_task_items.count %}
{% include "alerte/template_alerte.html" %}
</body>
</html>
project urls.py :
"""
URL configuration for tuto project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('menulateral/', include('menulateral.urls')),
path('menuvertical/', include('menuvertical.urls')),
path('alerte/', include('alerte.urls')),
path('filactu/', include('filactu.urls')),
path('core/', include('core.urls')),
path('taskboard/', include('taskboard.urls', namespace='taskboard')),
path('', include('logger.urls')),
]
Thank you in advance for your help !