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>