简体   繁体   English

用户提交表单后Django视图未呈现数据

[英]Django views not render data after user submit form

It sound simple but I want my page to display the database of my model. 听起来很简单,但我希望页面显示模型的数据库。

I use ModelForm for user to input and it would save into my model. 我使用ModelForm供用户输入,它将保存到我的模型中。 Now I want to render the whole table, not just each separately. 现在,我要渲染整个表格,而不仅仅是每个单独的表格。

forms.py forms.py

class My_Folio(forms.ModelForm):
    class Meta:
        model = my_data
        fields = ['symbol','buy_price','quantity']

views.py 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}) 

template: 模板:

{{instance}} This give me the user input after submit, but I want to show all of the inputs saved in database. {{instance}}提交后,这给了我用户输入,但我想显示数据库中保存的所有输入。

Maybe your post is invalid. 也许您的帖子无效。 Try this code in views.py . 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM