所以我正在為一家冰淇淋公司創建軟體,我想從訂單條目 HTML 中獲取客戶的資訊。但是當我填寫名字、姓氏、送貨地址等時,它在 Django admin 中顯示為空白。這是我的代碼:
表格.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',)
視圖.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})
訂單條目.html
<p>
<!--Basic Customer Information-->
<form method = "post">
{% csrf_token %}
{{ form.as_p }}
<button type = "submit">Place your order!</button>
</form>
</p>
模型.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 = [('PREFFERED','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)
這是管理員中的樣子

它在 python shell 中也是空白的。
我是 Django 的新手。任何幫助表示贊賞。謝謝。
uj5u.com熱心網友回復:
我還不能發表評論,所以我會在這里發表我的想法。我認為錯誤可能出現在您的欄位變數中。我認為表單欄位名稱需要匹配模型的屬性名稱,否則它不會將表單中的值與模型屬性配對。我可能是錯的,但這就是我的想法。因此,也許嘗試將您的表單欄位名稱與模型中的欄位名稱相匹配。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/337564.html
