Redirect is not working during form validation

HI all,

I am facing a issue in my django website. the issue is that after i submit the form the original URL(the URL which will lead to form and also which handles the form in the action tag) wil get loaded again although i have used redirect which should redirect to another page

views.py

def attendance(request):
  a=Engineer.objects.filter().values("EnggName")
  today= datetime.now()
  context={"form":AttendanceModelForm(),"a":a,"today":today}
  m=[]
  if request.method=="POST":
        form=AttendanceModelForm(request.POST or None)
        for course in a:
            m.append(course['EnggName'])
        if form.is_valid():
             Status_Loc_pairs = zip(m,request.POST.getlist('Status'), request.POST.getlist('Loc'))
             data_dicts = [{'Engg': Engg,'Status': Status, 'Loc': Loc} for Engg,Status, Loc in Status_Loc_pairs]
             a7=Attendance.objects.filter(currentdate=today).values("Status","Loc")
             if a7.exists():
                messages.info(request,f"There seems to be some data entered on {today} Please double check as it may lead to confusions")
                return redirect("check")
             else: 
              for data in data_dicts:
                form=AttendanceModelForm(data)
                #form.save()
              return HttpResponseRedirect("http://127.0.0.1:8000/attendance/completed/")
  else:
    return render(request,"attendance/attendance.html",context)

urls

from django.urls import path
from . import views


urlpatterns=[path('main/', views.attendance, name='attendance'),
             path('main_reuser/', views.test, name='test'),
             path('checkagain/', views.check, name='check'),
             path("completed/",views.completed, name="complete")]

html

{% load static %}
<html>
    <head>
        <title>hellaaa</title>
        <title>AttendanceSheet</title>
        <link rel="stylesheet" href="{% static 'dataview/bootstrap.min.css' %}">
        <script src="{% static 'dataview/jquery.min.js' %}"></script>
        <script src="{% static 'dataview/popper.min.js' %}"></script>
        <script src="{% static 'dataview/bootstrap.min.js' %}"></script>
        <script src="{% static 'newdata/htmx.min_1.9.5.js' %}"></script>
    </head>
    <body>
        <form hx-post="{% url 'attendance' %}"  id="test11" hx-trigger="click from:#submitall">
        {% csrf_token %}
        <table class="table table-bordered" id="attender">
            <thead>
                <th>Engineer</th>
                <th>Status</th>
                <th>Location</th>
            </thead>
        <tbody>
            {% for j in a %}
            <td>{{ j.EnggName }}</td>
            <td>{{ form.Status }}</td>
            <td>{{ form.Loc }}</td>
        </tr>
            {% endfor %}
        </tbody>
        </table>
    </form>
    </head>

INITIAL FORM

Form after submission

Although redirect was given in code it is staying in the same url but is printing contents of the form page and the redirect page("thanks for the data is part of the redirect page)

Please do not post images of code here - copy/paste the actual code into the text of your post, surrounded by lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

In general, seeing the same form being reloaded is an indication that the form_valid test failed for the form. The most common situation is because a required field was left empty, but there are other causes as well.

If you’re not rendering the errors in your form, then you should. See the docs at Working with forms | Django documentation | Django if you’re rendering individual fields manually.

Hi Ken,

Sorry for the mistake i have added the code in ``` and reposted. But Ken my form is inserting values in to the databse so if form errors where present would this be allowed

In the general case, you have complete control over what happens in the view. If you want to add data to the database even if the form is invalid, you can do that. Django doesn’t prevent you from doing what you may want to do.

In this specific case, no, you’re right. Now that I can read the code, I can see that an invalid form would not result in data being added.

Ahh - I just noticed you’re using HTMX. That’s a critical aspect of this that you should have highlighted up front.

HTMX does not do full page retrievals. When you do an HTMX post, it’s an AJAX call. Your URL doesn’t change - it gets the HTML returned from the request and replaces the corresponding HTML on the current page.