我有型号: 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')
我想按下用户按钮后增加一个点。我真的无法理解。
答案 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)