我正在写小型django网店,但我遇到了一个问题。也就是说,我想打印产品清单(我需要产品编号),还有一些表格可以在同一页上购买。
def order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
# POST data processing
return HttpResponseRedirect('/cart/')
else:
form = OrderForm()
# here I would like to also use product_id from POST to generate products
# that have such id
return render(request, 'order.html', {
'form': form, 'products': products,
})
但是当我错误地提交表单时,会调用OrderForm()然后我丢失了我的id。 任何提示如何解决这个问题(也许保持在某个id)?
编辑:我的商店用于在给定时间内租用一些产品,并将产品添加到购物车意味着我创建了新的对象租金,用于在用户预订产品时存储信息
型号:
class Rent(models.Model):
product = models.ForeignKey(Product)
paid = models.BooleanField()
begin_date = models.DateField(auto_now=False)
end_date = models.DateField(auto_now=False)
begin_hour = models.IntegerField(max_length=2, default=00)
end_hour = models.IntegerField(max_length=2, default=24)
和OrderForm
class OrderForm(forms.Form):
begin_date = forms.DateField(label='date', required=True,
widget=extras.widgets.SelectDateWidget(years=range(2013, 2015)))
end_date = forms.DateField(required=True, initial=datetime.date.today,
widget=extras.widgets.SelectDateWidget(years=range(2013, 2015)))
begin_hour = forms.ChoiceField(required=True, choices=((str(x), x) for x in range(0, 25)))
end_hour = forms.ChoiceField(required=True, choices=(((str(x)), x) for x in range(0, 25)))