我正在尝试创建一个简单的表单,通过在表单中输入记录的ID来检索该记录。
views.py
然而,在模板上当我使用
views.py
class Bill(models.Model):
"""
Represents a single purchase made by a customer at a business.
"""
business = models.ForeignKey(Businesses)
customer = models.ForeignKey(User, blank=True, null=True)
charge = models.ForeignKey(Charge, blank=True, null=True)
amount = models.IntegerField(verbose_name='Purchase amount (in cents)',
validators=[MinValueValidator(0)])
tip = models.IntegerField(verbose_name='Tip amount (in cents)',
validators=[MinValueValidator(0)])
comments = models.CharField(max_length=255, blank=True, null=True)
timestamp = models.DateTimeField(auto_now_add=True)
forms.py
从django导入表单 从django.forms导入ModelForm 从transactions.models导入账单(Bill)
class BillSelectForm(ModelForm):
class Meta:
model = Bill
fields = ('id',)
views.py
@login_required(login_url='/sign-in/')
def select_bill(request):
"""
Finds a bill based on its ID number.
"""
if request.method == 'POST':
form = BillSelectForm(request.POST)
if form.is_valid():
pass
# look up db for id number
# go to bill view page if id found AND bill not paid
# go back to same page and create message saying wrong bill
else:
form = BillSelectForm()
return render(request, 'bill_select.html', {'form': form})
然而,在模板上当我使用
{{ form.as_p }}
时,我什么都看不到。你有什么想法吗?
forms.Form
,因为ModelForm
会给出一个ValueError
,因为我们没有指定模型。 - halsdunes