'Django Python - Post not saving in Database Postgresql
I have an issue where my Django form is not posting into my Postgres database. Payload seems like all data is captured in variables. Here is my code if anyone can shine some light?
Form is posted and redirect back to form etc however data isn't passed onto the database. Have tried this as an AJAX post too but still the same, capturing variables but not passing into the database for some reason.
Views.py
def createResidential(request):
if request.method!="POST":
return HttpResponseRedirect(reverse("apps:customers.create"))
else:
residential_firstname=request.POST.get("residential_firstname")
residential_lasttname=request.POST.get("residential_lasttname")
residential_contact=request.POST.get("residential_contact")
residential_mobile=request.POST.get("residential_mobile")
residential_email=request.POST.get("residential_email")
residential_houseflatno=request.POST.get("residential_houseflatno")
residential_houseflatname=request.POST.get("residential_houseflatname")
residential_streetname=request.POST.get("residential_streetname")
residential_city=request.POST.get("residential_city")
residential_county=request.POST.get("residential_county")
residential_postcode=request.POST.get("residential_postcode")
try:
postResidential=ResidentialCustomer(
residential_firstname=residential_firstname,
residential_lasttname=residential_lasttname,
residential_contact=residential_contact,
residential_mobile=residential_mobile,
residential_email=residential_email,
residential_houseflatno=residential_houseflatno,
residential_houseflatname=residential_houseflatname,
residential_streetname=residential_streetname,
residential_city=residential_city,
residential_county=residential_county,
residential_postcode=residential_postcode,
)
postResidential.save()
messages.success(request,"Data Save Successfully")
return HttpResponseRedirect(reverse('apps:customers.create'))
except:
messages.error(request,"Error in Saving Data")
return HttpResponseRedirect(reverse('apps:customers.create'))
Models.py
class ResidentialCustomer(models.Model):
residential_firstname = models.TextField()
residential_lasttname = models.TextField()
residential_contact = models.IntegerField()
residential_mobile = models.IntegerField()
residential_email = models.EmailField()
residential_houseflatno = models.IntegerField()
residential_houseflatname = models.TextField()
residential_streetname = models.TextField()
residential_city = models.TextField()
residential_county = models.TextField()
residential_postcode = models.TextField()
residential_dateCreated = models.DateTimeField(default=timezone.now)
agent = models.ForeignKey(User, on_delete=models.PROTECT)
def __str__(self):
return f"{self.residential_firstname} {self.residential_lasttname}"
Form in html page
<form action="{% url 'apps:createResidential' %}" method="POST" class="vertical-navs-step">{% csrf_token %}
<div class="form-floating">
<input type="text" class="form-control" name="residential_firstname" placeholder="John">
<label for="residential_firstname">First Name</label>
</div>
<button type="submit" class="btn btn-success btn-label right ms-auto nexttab nexttab" data-nexttab="v-pills-finish-tab">
Same for all other fields I need to capture into the database. Appreciate the help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
