M
M
Mike2017-04-21 02:10:12
Django
Mike, 2017-04-21 02:10:12

How to save data for an authorized user?

I have
models.py model

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

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

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

views.py
def demo(request):
    if request.method == "POST":
        # тут я не понимаю как обратиться к "points" из модели "UserProfile"
        #что бы добавить 1 и  сохранить в базу
    return render(request, 'app/demo.html')

It is necessary for me that after clicking on the button in the HTML file, one point is added.
UPD:
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')

Issue resolved.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question