Hello! This is my first question, so I hope I get everything right, but I am using django forms and having an issue that my form is not validating and thus not sending my values to the database. It was working fine when I used {{ form.as_p }}, but when I changed it to code all the fields manually, then it stopped working.
This is my form html:
{% extends “daily_QA/base.html” %}
{% block content %}
Daily QA Test
{% csrf_token %}<label for="date_added">Select today's date: </label> </br> {{ dailytestform.date_added }} </br> </br> <label for="treatment-room-select">Select Treatment Room:</label> </br> <select name="treatment-room"> <option disabled selected value> -- select value -- </option> <option value="1" id="FBTR1opt">FBTR1</option> <option value="2" id="GTR2opt">GTR2</option> <option value="3" id="GTR3opt">GTR3</option> <option value="4" id="GTR4opt">GTR4</option> </select> <!-- {{ dailytestform.gantry }} {{ dailytestform.gantry.value }} --> </br> </br> <label for="lasers">Lasery: </label> {{ dailytestform.lasers }} </br> <label for="flatpanels_check">Flat panely: </label> {{ dailytestform.flatpanels_check }} </br> <label for="visionrt_check">VisionRt check: </label> {{ dailytestform.visionrt_check }} </br> <label for="dynr">DynR: </label> {{ dailytestform.dynr }} <!-- --> </br> <label for="temperature">Temperature: </label> </br> {{ dailytestform.temperature }} </br> <label for="pressure">Pressure: </label> </br> {{ dailytestform.pressure }} </br> </br> <input type="submit" value="Submit">
This is my forms.py:
class DailyTestForm(ModelForm):
date_added = forms.DateTimeField(widget=forms.DateTimeInput(format='%Y-%m-%d %H:%M', attrs={'type': 'date'})) gantry_choices=[(1, "FBTR1"),(2, "GTR2"),(3, "GTR3"),(4, "GTR4")] gantry = forms.IntegerField(widget=forms.Select(choices=gantry_choices)) visionrt_check = forms.BooleanField(label="Vision RT?") flatpanels_check = forms.BooleanField(label="Flat panely?") dynr = forms.BooleanField(label="DynR?") lasers = forms.BooleanField(label="Lasery x, y, z?") temperature = forms.DecimalField() pressure = forms.DecimalField() class Meta: model = DailyTest fields = ['date_added', 'visionrt_check', 'flatpanels_check', 'dynr', 'lasers']
And this is my views.py:
def daily(request):
submitted = False
form = DailyTestForm(request.POST)
form2 = DailyTestInputForm(request.POST)
if request.method == "POST": gantry_val = request.POST.get('treatment-room') temperature_val = request.POST.get('temperature') pressure_val = request.POST.get('pressure') print(request.POST.get('temperature')) print(gantry_val) form = DailyTestForm(request.POST) form2 = DailyTestInputForm(request.POST) if form.is_valid(): f = form.save(commit=False) f.gantry = gantry_val f.temperature = temperature_val f.pressure = pressure_val f.save() form.save_m2m()
print("Validated")
else:
print("Not validating") else: form = DailyTestForm(request.POST) form2 = DailyTestInputForm(request.POST) print("You are here") if 'submitted' in request.GET: #check whether form was submitted submitted = True return render(request, 'daily_QA/daily copy 2.html', { #context dictionary 'dailytestform':form, 'inputform': form2, 'submitted':submitted })
Now when I submit the form, the gantry and temperature values print, but it also prints “Not validating” and thus nothing shows up in the database.
Thank you in advance for your help!