Form.is_valid() always return False with no errors

I’m trying to submit a single form and can’t get it working properly. I’ve checked my code a couple of times looking for something weird/wrong but cannot see anything eye-catching. I’m new to Django so, I’m wondering if you guys can help me with that.

here is my view.py

def patientinformation(request):
    if request.user.is_authenticated:
        patient = PatientInformation.objects.get(user_name=request.user.username)
        if request.method == "POST":
            #patient = PatientInformation.objects.get(user_name=request.user.username)
            form = PatientInformationForm(request.POST or None, instance=patient)
            if form.is_valid():
                form.save()
                return redirect('patientinformation')
            else:
                return redirect('patientinformation')    
        else:
            #form = PatientInformationForm()
        
            #patient = PatientInformation.objects.get(user_name=request.user.username)
            form = PatientInformationForm(instance=patient)

            return render(request, 'patientinformation.html', {'form' : form})
        
    else:
        return redirect('login')  

Here is my model

class PatientInformation(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    emrpassid = models.CharField(max_length=50, default=uuid.uuid4, null=True, blank=True)
    first_name = models.CharField(max_length=50, null=True, blank=True)
    middle_name = models.CharField(max_length=50, null=True, blank=True)
    last_name = models.CharField(max_length=50, null=True, blank=True)
    dob = models.DateField(null=True, blank=True)
    street_address = models.CharField(max_length=100, null=True, blank=True)
    street_address2 = models.CharField(max_length=50, null=True, blank=True)
    postal_code = models.CharField(max_length=15, null=True, blank=True)
    city = models.CharField(max_length=50, null=True, blank=True)
    state = models.CharField(max_length=10, null=True, blank=True)
    home_phone = models.CharField(max_length=15, null=True, blank=True)
    work_phone = models.CharField(max_length=15, null=True, blank=True)
    mobile_phone = models.CharField(max_length=15, null=True, blank=True)
    emergency_contact = models.CharField(max_length=50, null=True, blank=True)
    relation_patient = models.CharField(max_length=50, null=True, blank=True)
    emergency_contact_phone = models.CharField(max_length=15, null=True, blank=True)
    user_name = models.CharField(max_length=50, null=False, blank=False)
    email = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return(f"{self.first_name} {self.last_name}")

here is my form


class PatientInformationForm(forms.ModelForm):
    emrpassid = forms.CharField(required=True, disabled=True, widget=forms.widgets.TextInput(attrs={"placeholder":"EMRPASS ID", "class":"form-control"}), label="EMRPASS ID")
    dob = forms.DateField(required=False, widget=forms.widgets.DateInput(attrs={"class":"form-control datepicker"}), label="DOB")
    email = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"E-mail", "class":"form-control"}), label="e-mail")
    first_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"First Name", "class":"form-control"}), label="First Name *")
    middle_name = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Middle Name", "class":"form-control"}), label="Middle Name")
    last_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Last Name", "class":"form-control"}), label="Last Name *")
    street_address = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Street Address", "class":"form-control"}), label="Street Address *")
    street_address2 = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Optional", "class":"form-control"}), label="Street Address 2")
    postal_code = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Zip Code", "class":"form-control"}), label="Zip Code *")
    city = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"City", "class":"form-control"}), label="City *")
    
    state = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"State", "class":"form-control"}), label="State")

    home_phone = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Home Phone", "class":"form-control"}), label="Home Phone")
    work_phone = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Work Phone", "class":"form-control"}), label="Work Phone")
    mobile_phone = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Mobile Phone", "class":"form-control"}), label="Mobile Phone")
    emergency_contact = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Emergency Contact", "class":"form-control"}), label="Emergency Contact")
    relation_patient = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Patient Relation", "class":"form-control"}), label="Patient Relation")
    emergency_contact_phone =forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"Emergency Contact", "class":"form-control"}), label="Emergency Contact Phone")

    class Meta:
        model = PatientInformation
        exclude = ("user",)

Any help would be really appreciated.

Thank you

I see a couple of possible / likely issues here.

First, in your view:

See the flow of events as documented at the view.


Next:

The user_name field in PatientInformation is not necessarily unique. It’s quite possible for there to be multiple instances of user_name with the same value. This will cause errors.

If these are references to your User model, then this field should be a ForeignKey to User. Additionally, if there is only one of these for each User, then it should be a OneToOneField and not a ForeignKey.


Next, I’m surprised that this doesn’t throw an error:

I don’t see a field named user in the PatientInformation model.

if form invalid, redicrect form page, not other url.

def patientinformation(request):
    if request.user.is_authenticated:
        patient = PatientInformation.objects.get(user_name=request.user.username)
        if request.method == "POST":
            #patient = PatientInformation.objects.get(user_name=request.user.username)
            form = PatientInformationForm(request.POST or None, instance=patient)
            if form.is_valid():
                form.save()
                return redirect('patientinformation')
            else:
                # return redirect('patientinformation')    
                ***return render(request, 'patientinformation.html', {'form' : form})***
        else:
            #form = PatientInformationForm()
        
            #patient = PatientInformation.objects.get(user_name=request.user.username)
            form = PatientInformationForm(instance=patient)

            return render(request, 'patientinformation.html', {'form' : form})
        
    else:
        return redirect('login')