python - Django 模型表单不保存 || 而是写入地址栏
问题描述
我目前有一个需要提交到数据库的客户详细信息表单。
但是,当我提交表单时,它不会像代码建议的那样保存或重定向。而是将所有字段写入页面的 URL 栏中。
模型.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 )
表格.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'
视图.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)
模板.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 %}
有谁知道是什么原因造成的,我已经尝试过缺少一些字段,并且所有字段都已满,所以我认为表格也无效
解决方案
问题是您没有进行任何发布请求
更改此
<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>
至
<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>
您应该将 url 替换为'the-url-of-newCustomer'
您的实际 url,默认情况下您可以将其设为空(这意味着该操作将是当前 url)。
推荐阅读
- react-native - 启动我的项目时出现 React Native Gradle 问题
- r - 您将如何对动物园对象列表中的相同列求和?
- php - 为什么这段代码的结果跳过了几个月?
- python - 仅从评估日起的债券现金流
- pentaho - PDI:如何在公式步骤中组合 IF 和 ISBLANK?
- r - 使用 ggplot2 和 geom_boxplot 手动定义框架的大小
- javascript - 如何使用nodejs在对话框流中的卡片响应中添加多个按钮?
- python - 反斜杠出现在文件路径中时是什么意思?
- python - 作为超级用户,我如何查看其他用户保存的对象?
- javascript - HTML/CSS 复选框文本与或对齐