Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flipping a boolean value with a simple view in Django?

Tags:

boolean

django

I have a simple view, but can't get it to do what it's supposed to, which is simply flip a Boolean Value:

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 "not", I've tried '-' and '!', but all to no avail.

like image 936
Michael Morisy Avatar asked Jul 30 '10 03:07

Michael Morisy


People also ask

How to flip a boolean value Python?

Use the numpy. logical_not() method to negate the boolean values in a numpy array, e.g. arr = np. array([True, True, False, False], dtype=bool) . The logical_not() method applies the logical not operator to the elements in the array and returns the result.

How do you reverse a boolean?

Press Ctrl+Shift+R and then choose Invert Boolean.

How do you set a Boolean variable to be false in Python?

If you want to define a boolean in Python, you can simply assign a True or False value or even an expression that ultimately evaluates to one of these values. You can check the type of the variable by using the built-in type function in Python.

How do you assign a value to a boolean?

Boolean variables are variables that can have only two possible values: true, and false. To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false.


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,
    })
like image 147
icktoofay Avatar answered Oct 05 '22 21:10

icktoofay


Late but may help someone else. You can toggle by using ^= True. Toggle active field in profile like:-

profile = request.user.get_profile()
profile.active ^= True
profile.save()
like image 34
Alexxio Avatar answered Oct 05 '22 19:10

Alexxio