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.
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.
Press Ctrl+Shift+R and then choose Invert Boolean.
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.
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.
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,
})
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With