我使用以下代码调用register_guest_order视图,但它不会在订单表单上预填充访客数据。
型号/形式:
class Order(models.Model):
guest = models.ForeignKey('hotel.Guest', on_delete=models.CASCADE)
date = models.DateField(auto_now_add=True)
amount = models.IntegerField()
item = models.ForeignKey('hotel.Item', on_delete=models.CASCADE)
price = models.IntegerField(default=1)
is_paid = models.BooleanField(default=False)
paid_date = models.DateField(blank=True, null=True)
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = ('guest', 'item', 'amount')
查看:
@login_required
def register_guest_order(request, *args, **kwargs):
if request.method == "POST":
form = RegisterGuestForm(request.POST, initial={'guest': kwargs['go']})
if form.is_valid():
guest = form.save(commit=False)
guest.save()
#post.published_date = timezone.now()
return redirect('guests')
else:
return redirect ('new_order')
context = {'form': form}
return redirect('guests')
URL:
url(r'^guest/orders/(?P<go>\d+)$', views.register_guest_order, name='new_guest_order'),
模板:
<a href="{% url 'new_guest_order' go=Guest.pk %}">