如何保存用户数据?

时间:2017-04-20 23:53:01

标签: python django

我有型号: models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile')
    points = models.IntegerField(default=0)

urls.py

urlpatterns = [
    url(r'^$', demo),
    url(r'^demo/$', demo),
]

demo.html

<form action="" method="POST">
    <button type="submit">+</button>
</form>

views.py

def demo(request):
    if request.method == "POST":
    # I don't understand how I can get "points" from the model   "UserProfile"
    #to add 1 and  to save in the DB
    return render(request, 'app/demo.html')

我想按下用户按钮后增加一个点。我真的无法理解。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

def demo(request):
    if request.method == "POST":
        profile = UserProfile.objects.get(user=request.user)
        profile.points += 1
        profile.save()
        return render(request, 'app/demo.html', {"user":request.user,
                                                 "profile":profile)