我正在用django表单为一家冰淇淋公司创建一个软件,我在我的django表单上出现在我网站的前端有麻烦。我可以显示客户信息,但是客户的订单没有显示,我很困惑发生了什么。
orderentry.html
<!--Basic Customer Information-->
<form method = "post">
{% csrf_token %}
{{ form.as_p }}
{{ newOrder}}
<button type = "submit">Place your order!</button>
</form>
views.py
from django.http.response import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.http import HttpResponse
import orderentry
from orderentry.forms import customerForm, customerInformation
def getCustomerInfo(request):
form = customerForm(request.POST)
if request.method == 'POST':
if form.is_valid():
form.save()
orderentry.forms.customer_first_name = form.cleaned_data['customer_first_name']
orderentry.forms.customer_last_name = form.cleaned_data['customer_last_name']
orderentry.forms.shipping_address = form.cleaned_data['shipping_address']
orderentry.forms.billing_address = form.cleaned_data['billing_address']
return redirect('/orderentry')
else:
form=customerForm()
return render(request, 'orderentry.html', {'form' : form})
def getCustomerOrder(request):
newOrder = customerInformation(request.POST)
if request.method == 'POST':
if newOrder.is_valid():
newOrder.save()
#orderentry.forms.order_Item_Flavor = newOrder.cleaned_data['order_Item_Flavor']
orderentry.forms.half_Pint_Count = newOrder.cleaned_data['half_Pint_Count']
orderentry.forms.one_Quart_Count = newOrder.cleaned_data['one_Quart_Count']
orderentry.forms.pint_Count = newOrder.cleaned_data['pint_Count']
orderentry.forms.half_Gallon_Count = newOrder.cleaned_data['half_Gallon_Count']
orderentry.forms.gallon_Count = newOrder.cleaned_data['gallon_Count']
orderentry.forms.cost = newOrder.cleaned_data['cost']
return redirect('/orderentry')
else:
print("error")
else:
newOrder=customerInformation()
return render(request, 'orderentry.html', {'newOrder' : newOrder})
forms.py
from django import forms
from django.forms import ModelForm
from orderentry.models import customerInfo, orderInfo
class customerForm(forms.ModelForm):
customer_first_name = forms.CharField(max_length=30)
customer_last_name = forms.CharField(max_length=30)
shipping_address = forms.CharField(max_length=60)
billing_address = forms.CharField(max_length=60)
class Meta:
model = customerInfo
fields = ('customer_first_name','customer_last_name','shipping_address', 'billing_address',)
class customerInformation(forms.ModelForm):
#order_Item_Flavor = forms.MultipleChoiceField(choices=['vanilla','chocolate','strawberry','cookiesncream'])
half_Pint_Count = forms.IntegerField()
one_Quart_Count = forms.IntegerField()
pint_Count = forms.IntegerField()
half_Gallon_Count = forms.IntegerField()
gallon_Count = forms.IntegerField()
cost = forms.IntegerField()
class Meta:
model = orderInfo
fields = ('half_Pint_Count', 'one_Quart_Count', 'pint_Count', 'half_Gallon_Count', 'gallon_Count', 'cost',)
models.py
class customerInfo (models.Model):
class Meta:
verbose_name = "Customer Information"
verbose_name_plural = "Customer Information"
customer_first_name = models.CharField(max_length=30)
customer_last_name = models.CharField(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)
class orderInfo (models.Model):
class Meta:
verbose_name = "Order Information"
verbose_name_plural = "Order Information"
order_Item_Flavor = models.ForeignKey('inventory.item', on_delete=models.CASCADE)
half_Pint_Count = models.IntegerField(default=0)
one_Quart_Count = models.IntegerField(default=0)
pint_Count = models.IntegerField(default=0)
half_Gallon_Count = models.IntegerField(default=0)
gallon_Count = models.IntegerField(default=0)
cost = models.IntegerField(default=0)
customer = models.ForeignKey(customerInfo, on_delete=models.CASCADE, default = 0)
def __str__(self):
return '%s, Half Pint: %s, Quart: %s, Pint: %s, Half Gallon: %s, Gallon: %s, $%s' % (self.order_Item_Flavor,
self.half_Pint_Count, self.one_Quart_Count, self.pint_Count, self.half_Gallon_Count, self.gallon_Count,
self.cost)
urls . py
from django.urls import path
from . import views
urlpatterns = [
path('', views.getCustomerInfo, name = 'orderentry'),
path('', views.getCustomerOrder, name = 'orderentry'),
基本上,这就是它看起来的样子,我不确定如何在前端显示customerInformation。前端
提前感谢任何帮助或指导,这是非常感激!
您可能认为getCustomerOrder
被正确调用,但当您访问orderentry.html
时它不会被调用(我不为这个文件提供url)。这意味着newOrder
没有传递到模板(您的orderentry.html
)。
newOrder
传递给你的模板getCustomerInfo
:
# inside getCustomerInfo func (I omitted other code)
else:
newOrder=customerInformation() # you used 'newOrder', so I'm using the same name. You can change it, of course.
form=customerForm()
return render(request, 'orderentry.html', {'form' : form, "newOrder": newOrder}) # notice! you have to put 'newOrder' in this dictionary (or context).
到目前为止,当用户点击"下订单"时,您的订单信息不会保存!"按钮。因此,您必须将getCustomerOrder
中if request.method == 'POST':
之后保存顺序的代码放入getCustomerInfo
中。