Data does not save to Postgresql

Hi Everyone,
I am having trouble save data from ModelForm to Postgresql database. It does not thrown any error but I cannot see any data in the Postgresql table. Below is my code, can someone point out what did I do wrong? Thank you

–Model Class–

from django.db import models


class Visitor(models.Model):
    name = models.CharField(max_length=100)
    whom_representing = models.CharField(max_length=100)
    whom_visiting = models.CharField(max_length=100)
    location = models.CharField(max_length=20)
    

    def __str__(self):
        return self.name

–urls.py–

from django.urls import path
from . import views
urlpatterns = [
    path('',views.admin_home, name='home'),    
    path('dashboard/',views.dashboard, name='dashboard'),    
    path('reports/',views.reports, name='reports'),    
    path('bookings/',views.bookings, name='bookings'),    
    path('visitor/', views.visitor_menu,name="visitor"),
    path('visitor_induction/', views.visitor_induction,name="visitor_induction"),
    path('visitor_sign_in/', views.visitor_sign_in,name="visitor_sign_in"),
    path('visitor_sign_out/', views.visitor_sign_out,name="visitor_sign_out"),
]

–views.py–

def visitor_sign_in(request):
    context = {}
    if request.method == 'POST':
        form = VisitorForm(request.POST)
        if form.is_valid():
            form.save()
            print('save')
        return redirect(visitor_menu)
    else:
        form = VisitorForm()
    context['form'] = form
    return render(request,'austpac_visitor_app/visitor_sign_in.html', context)

–template–

{% load static tailwind_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Visitor Check - In</title>
    {% tailwind_css %}
</head>
<div class="container mx-auto">
    <header>
        <h1 class='text-center bg-blue-400 text-white text-6xl' >Austpac - Visitor Sign-In</h1>
    </header>
    <body>
        
        <form action="{% url "visitor_sign_in" %}" method='POST'>
            {% csrf_token %}
            {{form}}
            <input type="submit" name="Submit" id="">
        </form>
        
        
    </body>
</div>
</html>

Welcome @letscodetpl !

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. (I have taken the liberty of correcting your original posts.
Please remember to do this in the future.)

Please post your VisitorForm definition.

1 Like

Hi Ken,
thanks for that, I will keep it in mind. below is my forms.py

from django import forms
from django.forms import ModelForm
from .models import Visitor

state_choices = {'VIC':'VIC','NSW':'NSW','WA':'WA'}

class VisitorForm(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Full Name','class':'font-bold'}))
    whom_representing = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Whom Representing'}))
    whom_visiting = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Whom Visiting'}))
    location = forms.MultipleChoiceField(widget = forms.Select, choices=state_choices)
    
    class Meta:
        model = Visitor
        fields = ['name','whom_representing','whom_visiting','location']

You do not need to import ModelForm from django.forms because you already have forms in the line before that

You are duplicating fields unknowingly. It is not necessary to explicitly declare those four field attributes (name, whom_representing, whom_visiting, location) in your VisitorForm class because ModelForm provides those for you. You subclassed ModelForm. ModelForm creates those fields for you from Visitor model automatically. You only need to adjust how they render (appearance). See the modified version below.

class VisitorForm(forms.ModelForm):
    class Meta:
        model = Visitor
        fields = "__all__"
        widgets = {
            "name": forms.TextInput(attrs={'placeholder':'Full Name','class':'font-bold'}),
            "whom_representing": forms.TextInput(attrs={'placeholder':'Whom Representing'}),
            "whom_visiting": forms.TextInput(attrs={'placeholder':'Whom Visiting'}),
            "location": form.SelectMultiple(choices=state_choices),
        }

Note that the widget for “location” is a form.SelectMultiple().

2 Likes

@onyeibo Thank you so much, it is working now.

2 Likes

Don’t forget to mark the thread as “Solved”. That would help those with similar issues find a solution. Cheers

1 Like