I currently have a customer details form that I need to submit to the database.
However when I submit the form it does not save nor redirect as the code suggests. It instead writes all the fields into the URL bar of the page.
Models.py:
from django.db import models
class newCustomerClass(models.Model):
customerName = models.CharField("Customer Name",max_length=50 , blank=True)
addressStreetNo = models.CharField(max_length=50 , blank=True)
addressStreet = models.CharField(max_length=50 , blank=True)
addressSuburb = models.CharField(max_length=50, blank=True )
addressCity = models.CharField(max_length=50, blank=True )
addressPO = models.CharField(max_length=50 , blank=True)
contact = models.CharField(max_length=50, blank=True )
mail = models.CharField(max_length=50, blank=True )
CellNo = models.CharField(max_length=50, blank=True )
Forms.py:
from django import forms
from .models import newCustomerClass
from django.forms import ModelForm
class newCustomerForm(ModelForm):
class Meta:
model = newCustomerClass
fields = 'customerName', 'addressStreetNo' ,'addressStreet', 'addressSuburb' ,'addressCity' , 'addressPO' , 'contact' , 'mail' , 'CellNo'
Views.py:
def newCustomer(request):
form = newCustomerForm()
if request.method == 'POST':
form = newCustomerForm(request.POST)
if form.is_valid():
form.save()
return redirect('home')
else:
print(form.errors)
content = {'form':form}
return render(request, 'main/newCustomer.html' , content)
Templates.html:
{% extends "main/base.html"%}
{% block content %}
<br>
<div class="container" style="text-align: center">
<form>
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.source.errors }}
{{ form.source }}
<h1 class="h1">New Customer</h1>
<br>
<h5>Customer Name {{ form.customerName }}</h5>
<br>
<h5>Street No {{ form.addressStreetNo }}</h5>
<h5>Street Name {{ form.addressStreet }}</h5>
<h5>Suburb {{ form.addressSuburb }}</h5>
<h5>City {{ form.addressCity }}</h5>
<br>
<h5>Contact {{ form.contact }}</h5>
<h5>E-Mail {{ form.mail }}</h5>
<h5>Cell {{ form.CellNo }}</h5>
<br>
<input type="submit" class="btn btn-success">
</form>
</div>
{% endblock %}
Does anyone know what causes this , I have tried to it with some fields missing , and all fields full so I don't think the form is not valid either
CodePudding user response:
the problem is that you are not making any post request
change this
<form>
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.source.errors }}
{{ form.source }}
<h1 class="h1">New Customer</h1>
<br>
<h5>Customer Name {{ form.customerName }}</h5>
<br>
<h5>Street No {{ form.addressStreetNo }}</h5>
<h5>Street Name {{ form.addressStreet }}</h5>
<h5>Suburb {{ form.addressSuburb }}</h5>
<h5>City {{ form.addressCity }}</h5>
<br>
<h5>Contact {{ form.contact }}</h5>
<h5>E-Mail {{ form.mail }}</h5>
<h5>Cell {{ form.CellNo }}</h5>
<br>
<input type="submit" class="btn btn-success">
</form>
to
<form method='post' action='the-url-of-newCustomer'>
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.source.errors }}
{{ form.source }}
<h1 class="h1">New Customer</h1>
<br>
<h5>Customer Name {{ form.customerName }}</h5>
<br>
<h5>Street No {{ form.addressStreetNo }}</h5>
<h5>Street Name {{ form.addressStreet }}</h5>
<h5>Suburb {{ form.addressSuburb }}</h5>
<h5>City {{ form.addressCity }}</h5>
<br>
<h5>Contact {{ form.contact }}</h5>
<h5>E-Mail {{ form.mail }}</h5>
<h5>Cell {{ form.CellNo }}</h5>
<br>
<input type="submit" class="btn btn-success">
</form>
you should replace the url 'the-url-of-newCustomer'
with your actual url and by default you can make it empty(so that means that the action will be the current url).