Moving a boolean with a simple view in Django?

I have a simple view, but I can’t get him to do what he suggested, that he simply flips the boolean:

def change_status(request):
 request.user.get_profile().active=not request.user.get_profile().active
 return render_to_response('holdstatus.html', {
  'user' : request.user,
 })

In addition to “no,” I tried “-” and “!”, But all to no avail.

+3
source share
2 answers

You need to save the changes to the database.

def change_status(request):
    profile = request.user.get_profile()
    profile.active = not profile.active
    profile.save()
    return render_to_response('holdstatus.html', {
       'user': request.user,
    })
+5
source

Late, but can help someone else. You can switch using ^= True. Switch field activeto profile as: -

profile = request.user.get_profile()
profile.active ^= True
profile.save()
0
source

Source: https://habr.com/ru/post/1757272/


All Articles