My form is inside this HTML, and essentially every time you click the button on the first page there are several forms presented in the page, the main problem is that when I submit the data for any of those forms, I get no error, but it actually doesn’t save the form data.
I have tested the individual form, and I managed to successfully update the database, so I’m not sure why when the form is rendered via add_to_forecast.html, the post is not even send
Django Views
## THIS VIEW PRESENTS THE FIRST HTML CODE
@login_required
def add_to_forecast(request):
id = request.GET.get('project_id')
request.session['lead_id'] = id
return render(request, 'account/lead_forecast.html')
## THIS GENERATES THE FORM
@login_required
def forecast_form(request):
if request.method=='POST':
form = LeadEntryForm(data=request.POST)
if form.is_valid():
form.save()
messages.success(request,"Successful Submission")
return redirect('add_to_forecast')
#Filling some information in the form
lead_id = request.session['lead_id']
data = {'lead_id':lead_id}
context = {
"form":LeadEntryForm(initial=data)
}
return render(request, 'account/lead_entry_forecast.html', context)
Are you saying you’re not even seeing the “POST” in your server log?
Can you check your browser’s developer tools, network tab, to verify that nothing is even trying to be sent?
That doesn’t seem to match up with what the docs say should be used in “hx-post”. I’m not seeing any reference in the docs for what setting hx-post="." is supposed to do.
My gut reaction is that this is going to try and post back to the page URL, not the form’s view url. (But that’s 100% conjecture with no idea as to the accuracy of that guess.)
Also, I don’t understand what you’re trying to show as the flow of events here.
What view presents the page?
What view presents the forms?
What view is supposed to be posted to by the forms?
Thanks for your prompt response, please see my answer below:
Correction - I can see the POST request in the server log:
[05/Dec/2021 11:01:48] “POST /account/add_to_forecast/ HTTP/1.1” 200 1898
The hx-post ="." is meant to force the post to stay in the view, if I remove that it will essentially refresh the page, ultimately, I want the user to see what he just submitted, but also to have the option to add additional data, obviously, at the moment, I can’t redirect the user to another HTML, as the form is not submitting data, eventually, that hx-post will point to another URL - If I removed the hx-post, the form still doesn’t work.
What view presents the page?
@login_required
def add_to_forecast(request):
id = request.GET.get('project_id')
request.session['lead_id'] = id
return render(request, 'account/lead_forecast.html')
What view presents the forms?
## THIS GENERATES THE FORM
@login_required
def forecast_form(request):
if request.method=='POST':
form = LeadEntryForm(data=request.POST)
if form.is_valid():
form.save()
messages.success(request,"Successful Submission")
return redirect('add_to_forecast')
#Filling some information in the form
lead_id = request.session['lead_id']
data = {'lead_id':lead_id}
context = {
"form":LeadEntryForm(initial=data)
}
What view is supposed to be posted to by the forms?
At the moment it points back to the main view - see image below
Side note: It looks like you’re creating multiple instances of the same form. In Django, this is typically a Use-case for a formset. You may have fewer issues if you build this around that instead of trying to manage multiple discrete forms.
The spilled knowledge from that tutorial is that if you are using HTMX:
"Ultimately, the solution to achieving dynamic form logic with Htmx is to not use formsets. As you’ve seen in this tutorial so far we haven’t used formsets at all when dealing with Htmx.
While this solution might not end up with exactly the result you were looking for, in my experience the things that matter are:
How understandable and maintainable is the code?
Does the desired outcome solve the problem?"
I didn’t actually use the tutorial code, basically, I had a similar problem to solve, so I use that information as my main source of “how-to”