Refactor NormalCaseForm to prevent nested forms and enhance logging in case_normal_form and create_case_normal views

This commit is contained in:
Ross
2025-11-13 22:13:46 +00:00
parent f8be1918f6
commit 970242e1fb
2 changed files with 19 additions and 1 deletions
+6 -1
View File
@@ -246,8 +246,13 @@ class NormalCaseForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
# Do not render a <form> tag from crispy here because the template
# already provides the surrounding form element (we inject this
# fragment into a modal that contains the form). Rendering a second
# <form> would nest forms and cause fields to be outside the
# submitted form (HTMX would only send the outer form's fields).
self.helper.form_method = "post"
self.helper.form_tag = True
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
Field("age_years", wrapper_class="col-md-4"),
+13
View File
@@ -631,7 +631,16 @@ def case_normal_form(request, pk):
"modality": inferred_modality,
})
# Log the rendered form HTML so we can verify inputs/names reach the client
from loguru import logger
try:
form_html = form.as_p()
logger.debug("case_normal_form rendered form HTML: {}", form_html)
except Exception as e:
logger.exception("Failed to render form.as_p(): {}", e)
html = render_to_string("atlas/partials/_normal_form.html", {"form": form, "case": case}, request=request)
logger.debug("case_normal_form returning HTML length: {}", len(html))
return HttpResponse(html)
@@ -648,8 +657,12 @@ def create_case_normal(request, pk):
return HttpResponse(status=403)
from .forms import NormalCaseForm
from loguru import logger
form = NormalCaseForm(request.POST)
logger.debug("create_case_normal POST data: {}", dict(request.POST))
if form.is_valid():
logger.debug("NormalCaseForm.cleaned_data: {}", form.cleaned_data)
nc = form.save(commit=False)
nc.case = case
nc.added_by = request.user