Django - How to increment integer field from user input?

前端 未结 2 555
长发绾君心
长发绾君心 2020-12-13 04:24

I\'m new to Django, but I\'m building a website where people can make statements regarding certain debatable topics. Right now I\'ve just manually added a few statements for

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 04:55

    You can get the user input with

    statements = request.POST.getlist('statement')
    

    From that list, you can increase the vote count and update database like

    for st in statements:
        statement = Statement.objects.get(id=st)
        statement.vote += 1
        statement.save()
    

    Hope this helps you.

提交回复
热议问题