我正在尝试在我的商店应用中进行订单注册,并且当我尝试在面板管理员中创建和保存订单时一切正常,但是页面上的订购问题,我制作的表格不想显示在页面上
order.html
{%extends 'shop/base.html'%}
{%load staticfiles%}
{%block title%}
Order
{%endblock%}
{% block content %}
<form class= 'form_order' method="post">
Zamówienie:
{% csrf_token %}
{{ form }}
<input type="submit" value="złóż zamówienie">
</form>
{% endblock %}
view.py
from django.shortcuts import render
from .forms import CreateOrderForm
from .models import OrderItem
from cart.cart import Cart
def Order_view(request):
cart=Cart(request)
if request.method== 'POST':
form=CreateOrderForm(request.POST)
if form.is_valid:
order= form.save()
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'] )
cart.clear()
return render(request, 'order/created_order.html', {order:'order'})
else:
form=CreateOrderForm()
return render(request, 'order/order.html', {cart:'cart', form: 'form' })
forms.py
from django.forms import ModelForm
from .models import Order
class CreateOrderForm(ModelForm):
class Meta:
model=Order
fields=['firstname', 'lastname', 'address', 'postal_code', 'city']
答案 0 :(得分:2)
您以错误的方式传递了上下文,您应该输入'key':value,而不是value:'key'。
context = {
'cart': cart,
'form': form
}
return render(request, 'order/order.html', context)