【发布时间】:2017-10-13 01:48:57
【问题描述】:
我正在尝试创建一个简单的表单,通过在表单中询问其 ID 来提取记录。
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 导入比尔
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 }} 时,我什么也看不到。想法?
【问题讨论】:
标签: python django forms django-forms