Django表单输入在Django管理中显示为空

2024-09-30 08:21:10 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在为一家冰淇淋公司创建软件,我想从订单输入HTML中获取客户信息。但当我填写名字、姓氏、送货地址等时,Django admin中会显示为空白。这是我的密码:

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',)

视图.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 = [('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)

下面是它在管理中的外观

enter image description here

它在python shell中也是空白的

我是Django的新手。感谢您的帮助。谢谢


Tags: namefromimportformmodelsformscustomerfirstname
1条回答
网友
1楼 · 发布于 2024-09-30 08:21:10

我还不能发表评论,所以我会在这里发表我的想法。我想这个错误可能是你的表单中的字段变量。我认为表单字段名需要与模型的属性名匹配,否则它将无法将表单中的值与模型属性配对。我可能错了,但我就是这么想的。所以,也许可以尝试将表单字段名和模型中的字段名匹配起来

相关问题 更多 >

    热门问题