RuntimeError Issue with Django When Using Forms and POST Redirections

Problem Description:

I am developing a Django application and I am encountering a RuntimeError when trying to submit a form. The error states: “You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set. Django can’t redirect to the slash URL while maintaining POST data.”

Technical Details:

  • asgiref 3.7.2 ASGI specs, helper code, and adapters
  • beautifulsoup4 4.12.2 Screen-scraping library
  • brotli 1.0.9 Python bindings for the Brotli compression library
  • dj-database-url 2.0.0 Use Database URLs in your Django Application.
  • django 4.1.3 A high-level Python web framework that encourages rapid development and clean, pragmatic design.
  • django-bootstrap-v5 1.0.11 Bootstrap 5 support for Django projects
  • django-ckeditor 6.5.1 Django admin CKEditor integration.
  • django-js-asset 2.0.0 script tag with additional attributes for django.forms.Media
  • djangorestframework 3.14.0 Web APIs for Django, made easy.
  • et-xmlfile 1.1.0 An implementation of lxml.xmlfile for the standard library
  • gunicorn 20.1.0 WSGI HTTP Server for UNIX
  • numpy 1.25.2 Fundamental package for array computing in Python
  • openpyxl 3.1.2 A Python library to read/write Excel 2010 xlsx/xlsm files
  • pandas 2.1.0 Powerful data structures for data analysis, time series, and statistics
  • pillow 9.3.0 Python Imaging Library (Fork)
  • psycopg2-binary 2.9.6 psycopg2 - Python-PostgreSQL Database Adapter
  • python-dateutil 2.8.2 Extensions to the standard Python datetime module
  • pytz 2023.3 World timezone definitions, modern and historical
  • six 1.16.0 Python 2 and 3 compatibility utilities
  • soupsieve 2.4.1 A modern CSS selector implementation for Beautiful Soup.
  • sqlparse 0.4.4 A non-validating SQL parser.
  • typing-extensions 4.6.3 Backported and Experimental Type Hints for Python 3.7+
  • tzdata 2023.3 Provider of IANA time zone data
  • whitenoise 6.5.0 Radically simplified static file serving for WSGI applications

Detailed Description:

The error occurs when I try to submit a messaging form in my application. This form is meant to send messages, either in the general context of the application or linked to a specific project (identified by projet_id).

Relevant Code:
Vue messagerie

@login_required(login_url='/user/')
def messagerie(request, projet_id=None):
    projet = None
    context = {}
    if projet_id:
        projet = get_object_or_404(Projet, pk=projet_id)
        context['projet_id'] = projet_id
    if request.method == 'POST':
        print('message POST')
        form = MessageForm(request.POST, request.FILES, user=request.user)
        if form.is_valid():
            objet = form.cleaned_data["objet"]
            messages = form.cleaned_data["messages"]
            recepteurs = form.cleaned_data["recepteurs"]
            msg = Message(
                objet=objet,
                messages=messages,
                emetteur=request.user,
                date_envoie=datetime.datetime.now(datetime.timezone.utc),
                status_envoie=True,
                projet = projet
            )
            msg.save()
            for user in recepteurs:
                msg.recepteurs.add(user)
                #send_notification_email(user, objet)
                
            #form = MessageForm(user=request.user)
            form = MessageForm(user=request.user, projet=projet) 
    else:
        print('message GET')
        #form = MessageForm(user=request.user)
        form = MessageForm(user=request.user, projet=projet) 
    #context = {'form': form}
    context['form'] = form
    template = loader.get_template('message.html')
    return HttpResponse(template.render(context, request))

** forms **

from django import forms
from .models import User
from ckeditor.widgets import CKEditorWidget



class MessageForm(forms.Form):
    objet = forms.CharField(max_length=200)
    messages = forms.CharField(required=False, widget=CKEditorWidget())
    recepteurs = forms.ModelMultipleChoiceField(
        queryset=User.objects.all(),
        required=False,
    )
    documents = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}), required=False)

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        projet = kwargs.pop('projet', None)
        super(MessageForm, self).__init__(*args, **kwargs)

        if user:
            self.fields['recepteurs'].queryset = User.objects.exclude(id=user.id)

        if projet:
            # Si un projet est spécifié, filtrez les utilisateurs en fonction de ce projet
            #self.fields['recepteurs'].queryset = projet.get_all_users().exclude(id=user.id) if user else projet.get_all_users()
            # Convertir les utilisateurs du projet en liste d'identifiants
            user_ids = [u.id for u in projet.get_all_users()]
            # Filtrer le queryset des récepteurs pour inclure uniquement les utilisateurs du projet
            self.fields['recepteurs'].queryset = User.objects.filter(id__in=user_ids)

urls.py (extraits) :

from django.urls import path
from . import views

app_name = 'mailmanagement'

urlpatterns = [
    # Ajouter 'projet_id' comme paramètre facultatif dans les URL
    path('<int:projet_id>/message/', views.messagerie, name="messagerie"),
    path('entrant/<int:projet_id>/', views.courier_entrant, name="entrant"),
    path('envoye/<int:projet_id>/', views.courier_envoye, name='courier_envoye'),
    path('projet/<int:projet_id>/', views.boitemessagerie, name="inbox_projet"),
    
    # Ajouter des URL sans 'projet_id' pour les cas où le projet n'est pas spécifié
    path('', views.boitemessagerie, name="inbox"),
    path('message/<int:message_id>/', views.detail_message, name='detail_message'),
    path('reply/<int:message_id>/', views.reply_to_message, name='reply_to_message'),
    path('message/', views.messagerie, name="messagerie_general"),
    path('entrant/', views.courier_entrant, name="entrant_general"),
    path('envoye/', views.courier_envoye, name='courier_envoye_general'),
]

template

{% extends "home.html" %}

{% block entete_messagerie %}   
    <div class="row container-fluid border border-warning">
        <div class="d-flex p-2 bd-highlight col-sm border border-success">
            {% load static %}
            {% if projet_id %}
                <!-- Lien vers la messagerie du projet -->
                <a href="{% url 'mailmanagement:messagerie' projet_id %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
            {% else %}
                <!-- Lien vers la messagerie générale -->
                <a href="{% url 'mailmanagement:messagerie_general' %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
            {% endif %}
            <form method="POST" action='{% if projet_id %} {% url "mailmanagement:messagerie" projet_id %}/ {% else %} {% url "mailmanagement:messagerie_general" %}/ {% endif %}' role="search" style="width: 15em; margin: 0.3em 2em;">
                {% csrf_token %}
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search">
                    <div class="input-group-btn">
                        <button type="submit" class="btn btn-default">
                            <span class="glyphicon glyphicon-search"></span>
                        </button>
                    </div>
                </div>
            </form> 
        </div>
    </div>                    
{% endblock %}

{% block menu %}
    {{ user.email }}
    <div class="list-group">
        {% load static %}
        {% if projet_id %}
            <a href="{% url 'mailmanagement:entrant' projet_id %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courrier entrant du Projet
            </a>
            <a href="#" class="list-group-item list-group-item-action"> <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" /> Brouillons du Projet</a>
            <a href="{% url 'mailmanagement:courier_envoye' projet_id %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />Courrier Envoyés du Projet</a>
            <a href="#" class="list-group-item list-group-item-action"><img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />Corbeille du Projet</a>
            <!-- Ajouter d'autres liens spécifiques au projet ici -->
        {% else %}
            <a href="{% url 'mailmanagement:entrant_general' %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courier entrant 
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" />
                Brouillons
            </a>
            <a href="{% url 'mailmanagement:courier_envoye_general' %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />
                Envoyés
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />
                Corbeille
            </a>
        {% endif %}
        <a href="/" class="list-group-item list-group-item-action">
            <img src="{% static 'images/menu.png' %}" alt="email" title="menu " />
            Return to Menu
        </a>

    </div>
{% endblock %}

Encountered Issues:

  • When submitting the form, I receive the aforementioned RuntimeError.
  • I have checked that all URLs in urls.py and in the template end with a slash (“/”).
  • I tried adding a slash at the end of the form action in the template, but this did not resolve the issue.


Here use something like this

{% url "mailmanagement:messagerie" projet_id=projet_id %}

The projet_id=projet_id part ensures that the value of projet_id is included in the URL as a parameter.

Also Remove the slashes that you have put in here.

One more thing in your settings.py have you set APPEND_SLASH = False

"I appreciate your suggestion to solve the form issue. However, when implementing your solution, I encounter a new NoReverseMatch error when projet_id is None. In my application, I need to handle two scenarios for messaging: a general mailbox and a mailbox linked to a specific project. Therefore, I need a way to distinguish between these two situations in my Django URL setup.

The issue arises because the {% url 'mailmanagement:messagerie' projet_id=projet_id %} tag tries to generate a URL for the messaging of a specific project even when projet_id is None. Do you have any suggestions on how to handle these two scenarios within my Django URL system?"

Thank you for your continued support and suggestions. Regarding the proposal to set APPEND_SLASH = False in settings.py , I have some reservations. Changing the APPEND_SLASH setting could potentially have broader implications on the URL behavior throughout my entire Django project. This change might not only affect the messaging system but also other parts of the application. I am looking for a solution that specifically addresses the URL handling for the messaging system without altering the global URL behavior of the Django project. It would be ideal if we could find a way to resolve the issue within the scope of the messaging system’s URLs and views. Do you have any recommendations that would allow for this specific focus?

Can you post what was actually rendered in the form tag on the page causing the error? I’m guessing that some of the spaces in your construction of the action attribute may be causing the problem.

I’d be more inclined to try wrapping the complete action attribute within the if tag.
e.g.
<form ... {% if ... %} action="{% url ... %}" {% else %} action="{% url %}" {% endif %} ...

Thank you for your suggestions. I applied this solution, but the problem persists.

<form method="POST" {% if projet_id %} action="{% url 'mailmanagement:messagerie' projet_id %}/" {% else %} action="{% url 'mailmanagement:messagerie_general' %}/" {% endif %} role="search" style="width: 15em; margin: 0.3em 2em;">

here the source code generated by the page before submission:


<!DOCTYPE html>
<html lang="fr">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    

    <link rel="stylesheet" href="/static/css/styles.css">
    
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js" integrity="sha256-6XMVI0zB8cRzfZjqKcD01PBsAy3FlDASrlC8SxCpInY=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/sortablejs@1.10.2/Sortable.min.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>


    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    
    <!-- Bootstrap icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css">
    <!-- Popper.js for Bootstrap -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <!-- Custom CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />

    

    
    
    <title> </title>
</head>
<body>
    <div class="container-fluid m-2 h-25">
        <div class="row">
            <div class="col-1 col-sm-1 d-flex justify-content-center justify-content-sm-start border border-secondary">
                <div style="width: 100px; height: 100px;">
                    
                    <img src="/static/images/logo.png" alt="BACOREX-SARL" class="img-fluid">
                </div>
                
            </div>
            <div class="col-10 col-sm-10 d-flex justify-content-center border border-success">
                
<style>
    .text-brown-light {
        color: #df7752; /* Marron clair */
        text-shadow: 2px 2px 4px #000000; /* Ombre noire pour le texte */
    }
    .text-brown-text {
        color: rgb(241, 236, 234); /* Marron clair */
        text-shadow: 2px 2px 4px #000000; /* Ombre noire pour le texte */
    }
    .bg-gradient {
        background-color: #6c757d;
        background-image: linear-gradient(315deg, #6c757d 0%, #d5d5d5 74%);
        /* Ajout d'une légère transparence pour le fond */
        opacity: 0.9; 
    }
</style>
<div class="container-fluid py-4 bg-gradient">
    <div class="row">
        <div class="col-md-6">
            <h2 class="text-brown-light display-4 font-weight-bold shadow-sm">BACOREX SARL</h2>
        </div>
        <div class="col-md-6 text-end">
            <span class="text-brown-text lead">Bienvenue, chef1</span>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <p class="text-brown-text fst-italic">Votre tableau de bord de gestion des projets</p>
        </div>
    </div>
</div>

            </div>
            <div class="col-1 col-sm-1 d-flex justify-content-center justify-content-sm-end border border-warning ">
                 
    <div class="dropdown" >
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
        
        
            <img src="/static/images/media/avatars/genetique.jpeg" alt="Avatar" class="rounded-circle" style="width:30px;height:30px;">
        
        <span class="ml-2">chef1</span>
    </button>

    <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
      <li><a class="dropdown-item" href="#"><i class="bi bi-person-fill me-2"></i>Votre Profile</a></li>
      <li><a class="dropdown-item" href="#"><i class="bi bi-gear-fill me-2"></i>Paramettre</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="/user/logout"><i class="bi bi-box-arrow-right me-2"></i>Se deconnecté</a></li>
    </ul>
</div>


            </div>
        </div>
    </div>
    
       
    <div class="row container-fluid border border-warning">
        <div class="d-flex p-2 bd-highlight col-sm border border-success">
            
            
                <!-- Lien vers la messagerie générale -->
                <a href="/boitereception/message/"> 
                    <img src="/static/images/write_email.png" alt="email" title="email" />
                </a>
            
            <form method="POST"  action="/boitereception/message/"  role="search" style="width: 15em; margin: 0.3em 2em;">
                <input type="hidden" name="csrfmiddlewaretoken" value="JbigjqrwvEVT4BczBg6XzF3LN1dyVc1LIrarPtab9OA4eyafDai9lclTEqFu4SXE">
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search">
                    <div class="input-group-btn">
                        <button type="submit" class="btn btn-default">
                            <span class="glyphicon glyphicon-search"></span>
                        </button>
                    </div>
                </div>
            </form> 
        </div>
    </div>                    

    <div class="row container-fluid border border-dark">
        <div class="col-12 col-sm-2 border border-success">
            
    chef1t@gmail.com
    <div class="list-group">
        
        
            <a href="/boitereception/entrant/" class="list-group-item list-group-item-action"> 
                <img src="/static/images/courrier-entrant.png" alt="email" title="email" />  Courier entrant 
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="/static/images/brouillon.png" alt="email" title="email" />
                Brouillons
            </a>
            <a href="/boitereception/envoye/" class="list-group-item list-group-item-action">
                <img src="/static/images/envoye.png" alt="email" title="email" />
                Envoyés
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="/static/images/corbeille.png" alt="email" title="email" />
                Corbeille
            </a>
        
        <a href="/" class="list-group-item list-group-item-action">
            <img src="/static/images/menu.png" alt="email" title="menu " />
            Return to Menu
        </a>

    </div>

        </div>
        <div class="col-12 col-sm-10 border border-dark overflow-scroll">
            
    <script src="/static/ckeditor/ckeditor.js"></script>
    <form action="/boitereception/message" method="post" enctype="multipart/form-data">
        <input type="hidden" name="csrfmiddlewaretoken" value="JbigjqrwvEVT4BczBg6XzF3LN1dyVc1LIrarPtab9OA4eyafDai9lclTEqFu4SXE">
        <script src="/static/ckeditor/ckeditor-init.js" data-ckeditor-basepath="/static/ckeditor/ckeditor/" id="ckeditor-init-script"></script>
<script src="/static/ckeditor/ckeditor/ckeditor.js"></script>
        <p>
    <label for="id_objet">Objet :</label>
    <input type="text" name="objet" maxlength="200" required id="id_objet">
    
    
  </p>

  
  <p>
    <label for="id_messages">Messages :</label>
    <div class="django-ckeditor-widget" data-field-id="id_messages" style="display: inline-block;">
    <textarea name="messages" cols="40" rows="10" id="id_messages" data-processed="0" data-config="{&quot;skin&quot;: &quot;moono-lisa&quot;, &quot;toolbar_Basic&quot;: [[&quot;Source&quot;, &quot;-&quot;, &quot;Bold&quot;, &quot;Italic&quot;]], &quot;toolbar_Full&quot;: [[&quot;Styles&quot;, &quot;Format&quot;, &quot;Bold&quot;, &quot;Italic&quot;, &quot;Underline&quot;, &quot;Strike&quot;, &quot;SpellChecker&quot;, &quot;Undo&quot;, &quot;Redo&quot;], [&quot;Link&quot;, &quot;Unlink&quot;, &quot;Anchor&quot;], [&quot;Image&quot;, &quot;Flash&quot;, &quot;Table&quot;, &quot;HorizontalRule&quot;], [&quot;TextColor&quot;, &quot;BGColor&quot;], [&quot;Smiley&quot;, &quot;SpecialChar&quot;], [&quot;Source&quot;]], &quot;toolbar&quot;: &quot;Full&quot;, &quot;height&quot;: 291, &quot;width&quot;: 835, &quot;filebrowserWindowWidth&quot;: 940, &quot;filebrowserWindowHeight&quot;: 725, &quot;language&quot;: &quot;fr-fr&quot;}" data-external-plugin-resources="[]" data-id="id_messages" data-type="ckeditortype"></textarea>
</div>
    
    
  </p>

  
  <p>
    <label for="id_recepteurs">Recepteurs :</label>
    <select name="recepteurs" id="id_recepteurs" multiple>
  <option value="6">operateur1 : Coordinateur des Operations</option>

  <option value="7">intervenant1 : Intervenant</option>

  <option value="3">daoud : Conducteurs des Travaux</option>

  <option value="2">Barhamou : Admin</option>

  <option value="5">conducteur1 : Conducteurs des Travaux</option>

  <option value="8">Amini : Chef Service Etude</option>

</select>
    
    
  </p>

  
  <p>
    <label for="id_documents">Documents :</label>
    <input type="file" name="documents" multiple id="id_documents">
    
    
      
    
  </p>
        <button type="submit"> Envoyer</button>
    </form>

        </div> 
    </div>
    <div id="footer" class="container-fluid border border-dark m-2">
        
            <footer class="footer bg-dark py-4"> <!-- Changement du fond à une couleur foncée pour un contraste élevé -->
  <div class="container">
      <div class="row">
          <div class="col text-center">
              <h5 class="text-white mb-2">BACOREX-SARL</h5> <!-- Converti le nom de la société en en-tête pour plus d'importance -->
              <p class="text-muted mb-1">&copy; 2023 Tous droits réservés.</p>
              <p class="text-muted">Réalisé par <a href="https://hamabarhamou.onrender.com/" class="text-primary">ISSAKA HAMA Barhamou</a>.</p> <!-- Mise en évidence du lien avec une couleur primaire pour une meilleure visibilité -->
          </div>
      </div>
      <div class="row mt-3"> <!-- Ajout d'une rangée supplémentaire pour d'éventuels liens ou informations supplémentaires -->
          <div class="col text-center">
              <a href="#" class="text-muted mx-2">Mentions légales</a> <!-- Exemple de liens supplémentaires -->
              <a href="#" class="text-muted mx-2">Politique de confidentialité</a>
          </div>
      </div>
  </div>
</footer>

        
    </div>
    <!-- Bootstrap JS bundle -->
    
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <!-- CKEditor JS -->
    <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
    <!-- Select2 JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
</body>
</html>

you can see that a / is missing at the end of the url. and what’s more, it’s not the right url.

<form action="/boitereception/message" method="post" enctype="multipart/form-data">

I don’t see anything wrong here.

In the rendered html you posted, I find:

                <!-- Lien vers la messagerie générale -->
                <a href="/boitereception/message/"> 
                    <img src="/static/images/write_email.png" alt="email" title="email" />
                </a>
            
            <form method="POST"  action="/boitereception/message/"  role="search" style="width: 15em; margin: 0.3em 2em;">
                <input type="hidden" name="csrfmiddlewaretoken" value="JbigjqrwvEVT4BczBg6XzF3LN1dyVc1LIrarPtab9OA4eyafDai9lclTEqFu4SXE">
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search">
                    <div class="input-group-btn">
                        <button type="submit" class="btn btn-default">
                            <span class="glyphicon glyphicon-search"></span>
                        </button>
                    </div>
                </div>
            </form> 

Which does have the right html based upon your template at:

                <!-- Lien vers la messagerie générale -->
                <a href="{% url 'mailmanagement:messagerie_general' %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
            {% endif %}
            <form method="POST" action='{% if projet_id %} {% url "mailmanagement:messagerie" projet_id %}/ {% else %} {% url "mailmanagement:messagerie_general" %}/ {% endif %}' role="search" style="width: 15em; margin: 0.3em 2em;">
                {% csrf_token %}
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search">
                    <div class="input-group-btn">
                        <button type="submit" class="btn btn-default">
                            <span class="glyphicon glyphicon-search"></span>
                        </button>
                    </div>
                </div>
            </form> 

Note: Since the template defines a trailing slash in the url, you should not be defining it in your url tag.

these are attempts at resolution, but nothing works. even with this new approach :slight_smile:

{% extends "home.html" %}

{% block entete_messagerie %}   
    <div class="row container-fluid border border-warning">
        <div class="d-flex p-2 bd-highlight col-sm border border-success">
            {% load static %}
            <!-- Choix de l'URL en fonction de la présence ou non de projet_id -->
            {% if projet_id %}
                <a href="{% url 'mailmanagement:messagerie' projet_id %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
                <!-- Construction de l'URL de la balise form pour messagerie liée au projet -->
                <form method="POST" action="{% url 'mailmanagement:messagerie' projet_id %}" role="search" style="width: 15em; margin: 0.3em 2em;">
            {% else %}
                <a href="{% url 'mailmanagement:messagerie_general' %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
                <!-- Construction de l'URL de la balise form pour messagerie générale -->
                <form method="POST" action="{% url 'mailmanagement:messagerie_general' %}" role="search" style="width: 15em; margin: 0.3em 2em;">
            {% endif %}
            {% csrf_token %}
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Search">
                <div class="input-group-btn">
                    <button type="submit" class="btn btn-default">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                </div>
            </div>
        </form> 
    </div>
</div>                    
{% endblock %}


{% block menu %}
    {{ user.email }}
    <div class="list-group">
        {% load static %}
        {% if projet_id %}
            <a href="{% url 'mailmanagement:entrant' projet_id %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courrier entrant du Projet
            </a>
            <a href="#" class="list-group-item list-group-item-action"> <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" /> Brouillons du Projet</a>
            <a href="{% url 'mailmanagement:courier_envoye' projet_id %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />Courrier Envoyés du Projet</a>
            <a href="#" class="list-group-item list-group-item-action"><img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />Corbeille du Projet</a>
            <!-- Ajouter d'autres liens spécifiques au projet ici -->
        {% else %}
            <a href="{% url 'mailmanagement:entrant_general' %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courier entrant 
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" />
                Brouillons
            </a>
            <a href="{% url 'mailmanagement:courier_envoye_general' %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />
                Envoyés
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />
                Corbeille
            </a>
        {% endif %}
        <a href="/" class="list-group-item list-group-item-action">
            <img src="{% static 'images/menu.png' %}" alt="email" title="menu " />
            Return to Menu
        </a>

    </div>
{% endblock %}

extrait url.py

path('<int:projet_id>/message/', views.messagerie, name="messagerie"),
path('message/', views.messagerie, name="messagerie_general"),

views:

@login_required(login_url='/user/')
def messagerie(request, projet_id=None):
    projet = None
    context = {}
    if projet_id:
        projet = get_object_or_404(Projet, pk=projet_id)
        context['projet_id'] = projet_id
    if request.method == 'POST':
        print('message POST')
        form = MessageForm(request.POST, request.FILES, user=request.user)
        if form.is_valid():
            objet = form.cleaned_data["objet"]
            messages = form.cleaned_data["messages"]
            recepteurs = form.cleaned_data["recepteurs"]
            msg = Message(
                objet=objet,
                messages=messages,
                emetteur=request.user,
                date_envoie=datetime.datetime.now(datetime.timezone.utc),
                status_envoie=True,
                projet = projet
            )
            msg.save()
            for user in recepteurs:
                msg.recepteurs.add(user)
                #send_notification_email(user, objet)
                
            #form = MessageForm(user=request.user)
            form = MessageForm(user=request.user, projet=projet) 
    else:
        print('message GET')
        #form = MessageForm(user=request.user)
        form = MessageForm(user=request.user, projet=projet) 
    #context = {'form': form}
    context['form'] = form
    template = loader.get_template('message.html')
    return HttpResponse(template.render(context, request))

Again, please show what has been rendered, and why it’s wrong.

In your previous post, the html rendered by this template was correct.

here the rendering of the page before submission that generated the error

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    

    <link rel="stylesheet" href="/static/css/styles.css">
    
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js" integrity="sha256-6XMVI0zB8cRzfZjqKcD01PBsAy3FlDASrlC8SxCpInY=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/sortablejs@1.10.2/Sortable.min.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>


    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    
    <!-- Bootstrap icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css">
    <!-- Popper.js for Bootstrap -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <!-- Custom CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />

    

    
    
    <title> </title>
</head>
<body>
    <div class="container-fluid m-2 h-25">
        <div class="row">
            <div class="col-1 col-sm-1 d-flex justify-content-center justify-content-sm-start border border-secondary">
                <div style="width: 100px; height: 100px;">
                    
                    <img src="/static/images/logo.png" alt="BACOREX-SARL" class="img-fluid">
                </div>
                
            </div>
            <div class="col-10 col-sm-10 d-flex justify-content-center border border-success">
                
<style>
    .text-brown-light {
        color: #df7752; /* Marron clair */
        text-shadow: 2px 2px 4px #000000; /* Ombre noire pour le texte */
    }
    .text-brown-text {
        color: rgb(241, 236, 234); /* Marron clair */
        text-shadow: 2px 2px 4px #000000; /* Ombre noire pour le texte */
    }
    .bg-gradient {
        background-color: #6c757d;
        background-image: linear-gradient(315deg, #6c757d 0%, #d5d5d5 74%);
        /* Ajout d'une légère transparence pour le fond */
        opacity: 0.9; 
    }
</style>
<div class="container-fluid py-4 bg-gradient">
    <div class="row">
        <div class="col-md-6">
            <h2 class="text-brown-light display-4 font-weight-bold shadow-sm">BACOREX SARL</h2>
        </div>
        <div class="col-md-6 text-end">
            <span class="text-brown-text lead">Bienvenue, chef1</span>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <p class="text-brown-text fst-italic">Votre tableau de bord de gestion des projets</p>
        </div>
    </div>
</div>

            </div>
            <div class="col-1 col-sm-1 d-flex justify-content-center justify-content-sm-end border border-warning ">
                 
    <div class="dropdown" >
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
        
        
            <img src="/static/images/media/avatars/genetique.jpeg" alt="Avatar" class="rounded-circle" style="width:30px;height:30px;">
        
        <span class="ml-2">chef1</span>
    </button>

    <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
      <li><a class="dropdown-item" href="#"><i class="bi bi-person-fill me-2"></i>Votre Profile</a></li>
      <li><a class="dropdown-item" href="#"><i class="bi bi-gear-fill me-2"></i>Paramettre</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="/user/logout"><i class="bi bi-box-arrow-right me-2"></i>Se deconnecté</a></li>
    </ul>
</div>


            </div>
        </div>
    </div>
    
       
    <div class="row container-fluid border border-warning">
        <div class="d-flex p-2 bd-highlight col-sm border border-success">
            
            <!-- Choix de l'URL en fonction de la présence ou non de projet_id -->
            
                <a href="/boitereception/1/message/"> 
                    <img src="/static/images/write_email.png" alt="email" title="email" />
                </a>
                <!-- Construction de l'URL de la balise form pour messagerie liée au projet -->
                <form method="POST" action="/boitereception/1/message/" role="search" style="width: 15em; margin: 0.3em 2em;">
            
            <input type="hidden" name="csrfmiddlewaretoken" value="eRDcsXEObreBOQBpovyOFitKBnYhMsILd7vnY0ntPBTMYNz5qpK0rPLSsMqdV8EE">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Search">
                <div class="input-group-btn">
                    <button type="submit" class="btn btn-default">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                </div>
            </div>
        </form> 
    </div>
</div>                    

    <div class="row container-fluid border border-dark">
        <div class="col-12 col-sm-2 border border-success">
            
    chef1t@gmail.com
    <div class="list-group">
        
        
            <a href="/boitereception/entrant/1/" class="list-group-item list-group-item-action"> 
                <img src="/static/images/courrier-entrant.png" alt="email" title="email" />  Courrier entrant du Projet
            </a>
            <a href="#" class="list-group-item list-group-item-action"> <img src="/static/images/brouillon.png" alt="email" title="email" /> Brouillons du Projet</a>
            <a href="/boitereception/envoye/1/" class="list-group-item list-group-item-action">
                <img src="/static/images/envoye.png" alt="email" title="email" />Courrier Envoyés du Projet</a>
            <a href="#" class="list-group-item list-group-item-action"><img src="/static/images/corbeille.png" alt="email" title="email" />Corbeille du Projet</a>
            <!-- Ajouter d'autres liens spécifiques au projet ici -->
        
        <a href="/" class="list-group-item list-group-item-action">
            <img src="/static/images/menu.png" alt="email" title="menu " />
            Return to Menu
        </a>

    </div>

        </div>
        <div class="col-12 col-sm-10 border border-dark overflow-scroll">
            
    <script src="/static/ckeditor/ckeditor.js"></script>
    <form action="/boitereception/message" method="post" enctype="multipart/form-data">
        <input type="hidden" name="csrfmiddlewaretoken" value="eRDcsXEObreBOQBpovyOFitKBnYhMsILd7vnY0ntPBTMYNz5qpK0rPLSsMqdV8EE">
        <script src="/static/ckeditor/ckeditor-init.js" data-ckeditor-basepath="/static/ckeditor/ckeditor/" id="ckeditor-init-script"></script>
<script src="/static/ckeditor/ckeditor/ckeditor.js"></script>
        <p>
    <label for="id_objet">Objet :</label>
    <input type="text" name="objet" maxlength="200" required id="id_objet">
    
    
  </p>

  
  <p>
    <label for="id_messages">Messages :</label>
    <div class="django-ckeditor-widget" data-field-id="id_messages" style="display: inline-block;">
    <textarea name="messages" cols="40" rows="10" id="id_messages" data-processed="0" data-config="{&quot;skin&quot;: &quot;moono-lisa&quot;, &quot;toolbar_Basic&quot;: [[&quot;Source&quot;, &quot;-&quot;, &quot;Bold&quot;, &quot;Italic&quot;]], &quot;toolbar_Full&quot;: [[&quot;Styles&quot;, &quot;Format&quot;, &quot;Bold&quot;, &quot;Italic&quot;, &quot;Underline&quot;, &quot;Strike&quot;, &quot;SpellChecker&quot;, &quot;Undo&quot;, &quot;Redo&quot;], [&quot;Link&quot;, &quot;Unlink&quot;, &quot;Anchor&quot;], [&quot;Image&quot;, &quot;Flash&quot;, &quot;Table&quot;, &quot;HorizontalRule&quot;], [&quot;TextColor&quot;, &quot;BGColor&quot;], [&quot;Smiley&quot;, &quot;SpecialChar&quot;], [&quot;Source&quot;]], &quot;toolbar&quot;: &quot;Full&quot;, &quot;height&quot;: 291, &quot;width&quot;: 835, &quot;filebrowserWindowWidth&quot;: 940, &quot;filebrowserWindowHeight&quot;: 725, &quot;language&quot;: &quot;fr-fr&quot;}" data-external-plugin-resources="[]" data-id="id_messages" data-type="ckeditortype"></textarea>
</div>
    
    
  </p>

  
  <p>
    <label for="id_recepteurs">Recepteurs :</label>
    <select name="recepteurs" id="id_recepteurs" multiple>
  <option value="6">operateur1 : Coordinateur des Operations</option>

  <option value="7">intervenant1 : Intervenant</option>

  <option value="3">daoud : Conducteurs des Travaux</option>

  <option value="2">Barhamou : Admin</option>

  <option value="4">chef1 : Chef de Projet</option>

</select>
    
    
  </p>

  
  <p>
    <label for="id_documents">Documents :</label>
    <input type="file" name="documents" multiple id="id_documents">
    
    
      
    
  </p>
        <button type="submit"> Envoyer</button>
    </form>

        </div> 
    </div>
    <div id="footer" class="container-fluid border border-dark m-2">
        
            <footer class="footer bg-dark py-4"> <!-- Changement du fond à une couleur foncée pour un contraste élevé -->
  <div class="container">
      <div class="row">
          <div class="col text-center">
              <h5 class="text-white mb-2">BACOREX-SARL</h5> <!-- Converti le nom de la société en en-tête pour plus d'importance -->
              <p class="text-muted mb-1">&copy; 2023 Tous droits réservés.</p>
              <p class="text-muted">Réalisé par <a href="https://hamabarhamou.onrender.com/" class="text-primary">ISSAKA HAMA Barhamou</a>.</p> <!-- Mise en évidence du lien avec une couleur primaire pour une meilleure visibilité -->
          </div>
      </div>
      <div class="row mt-3"> <!-- Ajout d'une rangée supplémentaire pour d'éventuels liens ou informations supplémentaires -->
          <div class="col text-center">
              <a href="#" class="text-muted mx-2">Mentions légales</a> <!-- Exemple de liens supplémentaires -->
              <a href="#" class="text-muted mx-2">Politique de confidentialité</a>
          </div>
      </div>
  </div>
</footer>

        
    </div>
    <!-- Bootstrap JS bundle -->
    
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <!-- CKEditor JS -->
    <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
    <!-- Select2 JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
</body>
</html>

Yes, and this url is correct.

yes, i notice them too. but i still don’t understand why i get this error:

RuntimeError at /boitereception/message

You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set. Django can’t redirect to the slash URL while maintaining POST data. Change your form to point to localhost:8000/boitereception/message/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

You must have a url somewhere else in one of your templates (either this one or the base template that it is extending) that is incorrect. Whatever the issue is, it is not at this form tag that you are currently looking at.

Okay, I see. I thought it was some kind of nomenclature:
app_name = 'mailmanagement'
avoided this kind of conflict?

It’s not a “conflict” in that sense of the term.

Please post your base template. Assuming the template you’re showing here is the complete file, then it’s something in the base causing the issue.

messageri.html

{% extends "home.html" %}

{% block entete_messagerie %}   
    <div class="row container-fluid border border-warning">
        <div class="d-flex p-2 bd-highlight col-sm border border-success">
            {% load static %}
            <!-- Choix de l'URL en fonction de la présence ou non de projet_id -->
            {% if projet_id %}
                <a href="{% url 'mailmanagement:messagerie' projet_id %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
                <!-- Construction de l'URL de la balise form pour messagerie liée au projet -->
                <form method="POST" action="{% url 'mailmanagement:messagerie' projet_id %}" role="search" style="width: 15em; margin: 0.3em 2em;">
            {% else %}
                <a href="{% url 'mailmanagement:messagerie_general' %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
                <!-- Construction de l'URL de la balise form pour messagerie générale -->
                <form method="POST" action="{% url 'mailmanagement:messagerie_general' %}" role="search" style="width: 15em; margin: 0.3em 2em;">
            {% endif %}
            {% csrf_token %}
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Search">
                <div class="input-group-btn">
                    <button type="submit" class="btn btn-default">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                </div>
            </div>
        </form> 
    </div>
</div>                    
{% endblock %}


{% block menu %}
    {{ user.email }}
    <div class="list-group">
        {% load static %}
        {% if projet_id %}
            <a href="{% url 'mailmanagement:entrant' projet_id %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courrier entrant du Projet
            </a>
            <a href="#" class="list-group-item list-group-item-action"> <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" /> Brouillons du Projet</a>
            <a href="{% url 'mailmanagement:courier_envoye' projet_id %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />Courrier Envoyés du Projet</a>
            <a href="#" class="list-group-item list-group-item-action"><img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />Corbeille du Projet</a>
            <!-- Ajouter d'autres liens spécifiques au projet ici -->
        {% else %}
            <a href="{% url 'mailmanagement:entrant_general' %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courier entrant 
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" />
                Brouillons
            </a>
            <a href="{% url 'mailmanagement:courier_envoye_general' %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />
                Envoyés
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />
                Corbeille
            </a>
        {% endif %}
        <a href="/" class="list-group-item list-group-item-action">
            <img src="{% static 'images/menu.png' %}" alt="email" title="menu " />
            Return to Menu
        </a>

    </div>
{% endblock %}

home.html

{% extends "base.html" %}

{% block entete %}
<style>
    .text-brown-light {
        color: #df7752; /* Marron clair */
        text-shadow: 2px 2px 4px #000000; /* Ombre noire pour le texte */
    }
    .text-brown-text {
        color: rgb(241, 236, 234); /* Marron clair */
        text-shadow: 2px 2px 4px #000000; /* Ombre noire pour le texte */
    }
    .bg-gradient {
        background-color: #6c757d;
        background-image: linear-gradient(315deg, #6c757d 0%, #d5d5d5 74%);
        /* Ajout d'une légère transparence pour le fond */
        opacity: 0.9; 
    }
</style>
<div class="container-fluid py-4 bg-gradient">
    <div class="row">
        <div class="col-md-6">
            <h2 class="text-brown-light display-4 font-weight-bold shadow-sm">BACOREX SARL</h2>
        </div>
        <div class="col-md-6 text-end">
            <span class="text-brown-text lead">Bienvenue, {{ user.username }}</span>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <p class="text-brown-text fst-italic">Votre tableau de bord de gestion des projets</p>
        </div>
    </div>
</div>
{% endblock %}

{% block profile %} 
    {% include "profile.html" %}
{% endblock %}

{% block menu %}
    {% include "menu.html" %}
{% endblock %}

base.html

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    {% load static %}

    <link rel="stylesheet" href="{% static 'css/styles.css' %}">
    
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js" integrity="sha256-6XMVI0zB8cRzfZjqKcD01PBsAy3FlDASrlC8SxCpInY=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/sortablejs@1.10.2/Sortable.min.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>


    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    
    <!-- Bootstrap icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css">
    <!-- Popper.js for Bootstrap -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <!-- Custom CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />

    

    {% block headfile %}
    {% endblock %}
    <title>{% block title %} {% endblock %}</title>
</head>
<body>
    <div class="container-fluid m-2 h-25">
        <div class="row">
            <div class="col-1 col-sm-1 d-flex justify-content-center justify-content-sm-start border border-secondary">
                <div style="width: 100px; height: 100px;">
                    {% load static %}
                    <img src="{% static 'images/logo.png' %}" alt="BACOREX-SARL" class="img-fluid">
                </div>
                
            </div>
            <div class="col-10 col-sm-10 d-flex justify-content-center border border-success">
                {% block entete %} 
                    <!-- En-tête -->
                {% endblock %}
            </div>
            <div class="col-1 col-sm-1 d-flex justify-content-center justify-content-sm-end border border-warning ">
                {% block profile %} 
                    <!-- Profil -->
                {% endblock %}
            </div>
        </div>
    </div>
    
    {% block entete_messagerie %} 
    {% endblock %}
    <div class="row container-fluid border border-dark">
        <div class="col-12 col-sm-2 border border-success">
            {% block menu %} 
            {% endblock %}
        </div>
        <div class="col-12 col-sm-10 border border-dark overflow-scroll">
            {% block content %} {% endblock %}
        </div> 
    </div>
    <div id="footer" class="container-fluid border border-dark m-2">
        {% block footer %}
            {% include "footer.html" %}
        {% endblock %}
    </div>
    <!-- Bootstrap JS bundle -->
    {% block codejs %}
    {% endblock %}
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <!-- CKEditor JS -->
    <script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
    <!-- Select2 JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
</body>
</html>

In the rendered HTML you posted at RuntimeError Issue with Django When Using Forms and POST Redirections - #12 by HamaBarhamou, I see this:

    <script src="/static/ckeditor/ckeditor.js"></script>
    <form action="/boitereception/message" method="post" enctype="multipart/form-data">
        <input type="hidden" name="csrfmiddlewaretoken" value="eRDcsXEObreBOQBpovyOFitKBnYhMsILd7vnY0ntPBTMYNz5qpK0rPLSsMqdV8EE">
        <script src="/static/ckeditor/ckeditor-init.js" data-ckeditor-basepath="/static/ckeditor/ckeditor/" id="ckeditor-init-script"></script>
<script src="/static/ckeditor/ckeditor/ckeditor.js"></script>
        <p>
    <label for="id_objet">Objet :</label>
    <input type="text" name="objet" maxlength="200" required id="id_objet">

However, I’m not seeing where this batch of HTML is coming from in the set of templates you’ve provided here.

Hello everyone,

I wanted to express my gratitude for the assistance and advice you provided regarding the issue I was facing with forms in my Django application. Thanks to your suggestions and some debugging, I was finally able to find a solution.

The issue was actually due to a mix-up between two templates: messagerie.html and message.html. Upon closer examination of my code, I realized that the forms in these two templates were not properly configured, leading to incorrect POST requests.

In messagerie.html, I adjusted the <form> tag to correctly point to either the project-linked messaging URL or the general messaging URL, depending on whether the project ID (projet_id) was present.

In message.html, which extends messagerie.html, I made similar adjustments to ensure that form data was sent to the correct URL.

Thanks to these changes, my application now correctly handles POST requests for messaging forms, whether for general messaging or messaging linked to a specific project.

I am truly grateful for your support and advice. It shows how valuable a community can be when it comes to solving complex technical problems.

Thank you all again!

Best regards,

messagerie.html

{% extends "home.html" %}

{% block entete_messagerie %}   
    <div class="row container-fluid border border-warning">
        <div class="d-flex p-2 bd-highlight col-sm border border-success">
            {% load static %}
            {% if projet_id %}
                <a href="{% url 'mailmanagement:messagerie' projet_id %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
            {% else %}
                <a href="{% url 'mailmanagement:messagerie_general' %}"> 
                    <img src="{% static 'images/write_email.png' %}" alt="email" title="email" />
                </a>
            {% endif %}
            <form method="POST" action="{% url 'mailmanagement:messagerie_general' %}" role="search" style="width: 15em; margin: 0.3em 2em;"></form>
            {% csrf_token %}
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Search">
                <div class="input-group-btn">
                    <button type="submit" class="btn btn-default">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                </div>
            </div>
        </form> 
    </div>
</div>                    
{% endblock %}


{% block menu %}
    {{ user.email }}
    <div class="list-group">
        {% load static %}
        {% if projet_id %}
            <a href="{% url 'mailmanagement:entrant' projet_id %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courrier entrant du Projet {{ projet_id }}
            </a>
            <a href="#" class="list-group-item list-group-item-action"> <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" /> Brouillons du Projet</a>
            <a href="{% url 'mailmanagement:courier_envoye' projet_id %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />Courrier Envoyés du Projet</a>
            <a href="#" class="list-group-item list-group-item-action"><img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />Corbeille du Projet</a>
            <!-- Ajouter d'autres liens spécifiques au projet ici -->
        {% else %}
            <a href="{% url 'mailmanagement:entrant_general' %}" class="list-group-item list-group-item-action"> 
                <img src="{% static 'images/courrier-entrant.png' %}" alt="email" title="email" />  Courier entrant 
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/brouillon.png' %}" alt="email" title="email" />
                Brouillons
            </a>
            <a href="{% url 'mailmanagement:courier_envoye_general' %}" class="list-group-item list-group-item-action">
                <img src="{% static 'images/envoye.png' %}" alt="email" title="email" />
                Envoyés
            </a>
            <a href="#" class="list-group-item list-group-item-action">
                <img src="{% static 'images/corbeille.png' %}" alt="email" title="email" />
                Corbeille
            </a>
        {% endif %}
        <a href="/" class="list-group-item list-group-item-action">
            <img src="{% static 'images/menu.png' %}" alt="email" title="menu " />
            Return to Menu
        </a>

    </div>
{% endblock %}

message.html

{% extends "messagerie.html" %}
{% load static %}

{% block content %}
    <script src="{% static 'ckeditor/ckeditor.js' %}"></script>
    <!-- <form action="/boitereception/message" method="post" enctype="multipart/form-data"> -->
    <!-- Choix de l'URL en fonction de la présence ou non de projet_id -->
    {% if projet_id %}
    <!-- Construction de l'URL de la balise form pour messagerie liée au projet -->
    <form method="POST" action="{% url 'mailmanagement:messagerie' projet_id %}" role="search" style="width: 15em; margin: 0.3em 2em;">
    {% else %}
        <!-- Construction de l'URL de la balise form pour messagerie générale -->
        <form method="POST" action="{% url 'mailmanagement:messagerie_general' %}" role="search" style="width: 15em; margin: 0.3em 2em;">
    {% endif %}

        {% csrf_token %}
        {{ form.media }}
        {{ form.as_p }}
        <button type="submit"> Envoyer</button>
    </form>
{% endblock %}