Django 表单输入在 Django Admin 中显示为空白
Django Form inputs show up as blank in Django Admin
我正在为一家冰淇淋公司开发软件,我想从订单条目中获取客户信息 HTML。但是当我填写名字、姓氏、送货地址等时,它在 Django 管理中显示为空白。这是我的代码:
forms.py
from django import forms
from orderentry.models import customerInfo, orderInfo
class customerForm(forms.ModelForm):
firstName = forms.CharField(max_length=30)
lastName = forms.CharField(max_length=30)
shippingAddress = forms.CharField(max_length=60)
billingAddress = forms.CharField(max_length=60)
class Meta:
model = customerInfo
fields = ('firstName','lastName','shippingAddress', 'billingAddress',)
views.py
from django.http import HttpResponse
import orderentry
from orderentry.forms import customerForm
def getCustomerInfo(request):
form = customerForm(request.POST)
if request.method == 'POST':
if form.is_valid():
form.save()
orderentry.forms.firstName = form.cleaned_data['firstName']
orderentry.forms.lastName = form.cleaned_data['lastName']
orderentry.forms.shippingAddress = form.cleaned_data['shippingAddress']
orderentry.forms.billingAddress = form.cleaned_data['billingAddress']
return redirect('/orderentry')
else:
form=customerForm()
return render(request, 'orderentry.html', {'form' : form})
orderentry.html
<p>
<!--Basic Customer Information-->
<form method = "post">
{% csrf_token %}
{{ form.as_p }}
<button type = "submit">Place your order!</button>
</form>
</p>
models.py
from django.db import models
from inventory.models import item, sizeCounts
import uuid
class customerInfo (models.Model):
class Meta:
verbose_name = "Customer Information"
verbose_name_plural = "Customer Information"
customer_first_name = models.CharField(blank=True, max_length=30)
customer_last_name = models.CharField(blank=True, max_length=30)
shipping_address = models.CharField(max_length=60)
billing_address = models.CharField(max_length=60)
customer_status_choices = [('PREFERRED', 'preferred'), ('OKAY', 'okay'), ('SHAKY', 'shaky')]
customer_status = models.CharField(max_length=30, choices = customer_status_choices, default="PREFERRED")
def __str__(self):
return '%s %s' % (self.customer_first_name, self.customer_last_name)
这是它在管理中的样子
pythonshell也是空白
我是 Django 的新手。任何帮助表示赞赏。谢谢。
我还不能发表评论,所以我会post在这里发表我的想法。我认为错误可能出在您的表单中,您的字段变量。我认为表单字段名称需要与模型的属性名称相匹配,否则它不会将表单中的值与模型属性配对。我可能是错的,但这就是我的想法。因此,也许尝试将您的表单字段名称与模型中的字段名称相匹配。
我正在为一家冰淇淋公司开发软件,我想从订单条目中获取客户信息 HTML。但是当我填写名字、姓氏、送货地址等时,它在 Django 管理中显示为空白。这是我的代码:
forms.py
from django import forms
from orderentry.models import customerInfo, orderInfo
class customerForm(forms.ModelForm):
firstName = forms.CharField(max_length=30)
lastName = forms.CharField(max_length=30)
shippingAddress = forms.CharField(max_length=60)
billingAddress = forms.CharField(max_length=60)
class Meta:
model = customerInfo
fields = ('firstName','lastName','shippingAddress', 'billingAddress',)
views.py
from django.http import HttpResponse
import orderentry
from orderentry.forms import customerForm
def getCustomerInfo(request):
form = customerForm(request.POST)
if request.method == 'POST':
if form.is_valid():
form.save()
orderentry.forms.firstName = form.cleaned_data['firstName']
orderentry.forms.lastName = form.cleaned_data['lastName']
orderentry.forms.shippingAddress = form.cleaned_data['shippingAddress']
orderentry.forms.billingAddress = form.cleaned_data['billingAddress']
return redirect('/orderentry')
else:
form=customerForm()
return render(request, 'orderentry.html', {'form' : form})
orderentry.html
<p>
<!--Basic Customer Information-->
<form method = "post">
{% csrf_token %}
{{ form.as_p }}
<button type = "submit">Place your order!</button>
</form>
</p>
models.py
from django.db import models
from inventory.models import item, sizeCounts
import uuid
class customerInfo (models.Model):
class Meta:
verbose_name = "Customer Information"
verbose_name_plural = "Customer Information"
customer_first_name = models.CharField(blank=True, max_length=30)
customer_last_name = models.CharField(blank=True, max_length=30)
shipping_address = models.CharField(max_length=60)
billing_address = models.CharField(max_length=60)
customer_status_choices = [('PREFERRED', 'preferred'), ('OKAY', 'okay'), ('SHAKY', 'shaky')]
customer_status = models.CharField(max_length=30, choices = customer_status_choices, default="PREFERRED")
def __str__(self):
return '%s %s' % (self.customer_first_name, self.customer_last_name)
这是它在管理中的样子
pythonshell也是空白
我是 Django 的新手。任何帮助表示赞赏。谢谢。
我还不能发表评论,所以我会post在这里发表我的想法。我认为错误可能出在您的表单中,您的字段变量。我认为表单字段名称需要与模型的属性名称相匹配,否则它不会将表单中的值与模型属性配对。我可能是错的,但这就是我的想法。因此,也许尝试将您的表单字段名称与模型中的字段名称相匹配。