听起来很简单,但我希望页面显示模型的数据库。
我使用ModelForm供用户输入,它将保存到我的模型中。现在,我要渲染整个表格,而不仅仅是每个单独的表格。
forms.py
class My_Folio(forms.ModelForm):
class Meta:
model = my_data
fields = ['symbol','buy_price','quantity']
views.py
def blockfolio(request):
if request.method == 'POST':
my_folio = My_Folio(request.POST)
if my_folio.is_valid():
symbol = my_folio.cleaned_data['symbol']
buy_price = my_folio.cleaned_data['buy_price']
quantity = my_folio.cleaned_data['quantity']
instance = my_folio.save(commit=False)
instance.save()
return render(request, 'blockfolio/blockfolio.html', {'symbol':symbol, 'buy_price':buy_price, 'quantity':quantity, 'instance':instance})
模板:
{{instance}}
这给了我用户提交后的输入,但是我想显示数据库中保存的所有输入。
答案 0 :(得分:0)
也许您的帖子无效。在views.py
中尝试此代码。
def blockfolio(request):
if request.method == 'POST':
my_folio = My_Folio(request.POST)
if my_folio.is_valid():
symbol = my_folio.cleaned_data['symbol']
buy_price = my_folio.cleaned_data['buy_price']
quantity = my_folio.cleaned_data['quantity']
instance = my_folio.save(commit=False)
instance.save()
return render(request, 'blockfolio/blockfolio.html', {'symbol':symbol, 'buy_price':buy_price, 'quantity':quantity, 'instance':instance})
else:
print("request is invalid: {}".format(request.POST))
return "bad request!", 500