I’m facing a strange issue with Django. I have set up a simple form in a template that submits to a view, but I’m not getting the expected 404 response. Instead, I’m being redirected to the URL http://localhost:8000/clear/ without seeing the 404 error.
Here is my setup:
Template:
<form method="post" action="{% url 'clear_chat' %}">
{% csrf_token %}
<button type="submit" class="btn btn-danger clear-button me-2">
Clear Chat
</button>
</form>
View:
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
@login_required
def clear_chat(request):
return HttpResponse("Invalid request method.", status=404)
Urls:
from django.urls import path
from .views import clear_chat
urlpatterns = [
path('clear/', clear_chat, name='clear_chat'),
]
Instead of seeing the 404 response, I’m redirected to the URL http://localhost:8000/clear/ without any error message. I have no clue what is wrong with that code.