Form not validating when selecting only some fields from model

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>
{{ dailytestform.date_added }}

<label for="treatment-room-select">Select Treatment Room:</label>
<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>

    {{ dailytestform.gantry }}

    {{ dailytestform.gantry.value }} -->

<label for="lasers">Lasery: </label> 
{{ dailytestform.lasers }}
<label for="flatpanels_check">Flat panely: </label>
{{ dailytestform.flatpanels_check }}

<label for="visionrt_check">VisionRt check: </label>
{{ dailytestform.visionrt_check }}
<label for="dynr">DynR: </label>
{{ dailytestform.dynr }}

<!--  -->
<label for="temperature">Temperature: </label>
{{ dailytestform.temperature }}

<label for="pressure">Pressure: </label>
{{ dailytestform.pressure }}

<input type="submit" value="Submit">

This is my

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

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')

    form = DailyTestForm(request.POST)
    form2 = DailyTestInputForm(request.POST)

    if form.is_valid():
        f =
        f.gantry = gantry_val
        f.temperature = temperature_val
        f.pressure = pressure_val
        print("Not validating")

    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
    'inputform': form2,

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!

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

See Working with forms | Django documentation | Django for information on manually rendering fields with any error messages to see why is_valid would be returning false.

You might also want to compare the html that is rendered in both cases (the working and non-working versions) along with verifying that what is submitted in the POST request is the same for both.

1 Like

Check if any selected field are marked as a required in the model, if so, ensure that those required fields are not also included in the form submission. if they are not included the validator will fail. either including th require field in the form or modify the model to make them optional.

This worked! I put one of the fields in the “exlude” when creating the form, so I thought it was handled, but when I compared the data that comes and the fields expected, this field was actually still there. Making it required=False made it work, so thanks!

Thanks for the tip re formatting! I will use it next time