【发布时间】:2021-10-26 20:58:13
【问题描述】:
我正在使用 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 显示在前端。 Front End
提前感谢您的任何帮助或指导,非常感谢!
【问题讨论】:
-
请出示您的
urls.py。 -
@bichanna 刚刚更新!
-
你的问题解决了吗?
-
对不起!是的,问题已解决,感谢您的帮助
标签: python django django-models django-views django-forms