how to remove an item from django session? - python

I do have two items in request.session.get('product_key'). I am trying to delete one product.
m = request.session['product_key']
m.remove('768')
when i am trying to delete one product from the session its getting deleted from the variable but not from actual session. when i am printing m its giving me single product while i am typing request.session.get('product_key') its giving me two products.
So, how can i achieve so?
Edit:
I dont want to delete complete session, i want to delete one variable from the session,i have 2 items in one key name.
print(request.session['product_key']) = ['123','768']

If you modify a list in a session value - rather than changing or replacing the value completely - Django will not automatically know it needs to save the session. You need to tell it explicitly:
request.session.modified = True
See the docs on When sessions are saved.

try :
del request.session['your key']
or
m = request.session.pop('your key')

Here you go;
request.session["your key"].pop(index of the element you want to remove)
request.session.modified = True

Related

How to filtering in Django use specific user

I try to filtering django some specific user. I try with list but it not working. Do you have any solution.
list = ['bill gates','elon musk','aamir khan','larry page']
allPosts = Post.objects.filter(author=list)
When I change list filter can work dynamically
You can use __in:
my_list = ['bill gates','elon musk','aamir khan','larry page']
allPosts = Post.objects.filter(author__in=my_list)
Note: Never use python built in functions (Ex: list) as variable names. It would be better to avoid the need for this by choosing a different variable name.

Is there anyway to delete object from queryset that already chosen in django?

Example:
query = Test.objects.all()
I want to delete the specific one.
But before deletion I want to do some function like so.
for q in query:
if q.name == something:
newq = q.relate_set.all()
query.remove(q) # this remove not work
I just figure it out.
Just change it to list type and then delete it.
And I can use those elements in the list like queryset.And I don't know why.
Can anyone explain?
Thanks anyway.

Django insert into list if doesn't contain it

I want to insert into the notifications list, one notification of each type. and I have this:
Result from the initial query (I guess is a list with queryset), named notifications:
[<Notification: Should be first>, <Notification: New link>]
And the restriction that I have is:
for(note in notification):
if len(note.region.all()) == 0 and (note.notificationType.priority not in notifications):
notifications.append(note)
My question is, how do I get inside the notifications list to get the attribute notificationType.priority to see is that number isn't inside notifications list.
If I get your question, you can try this :
notificationsPiorities = set([note.notificationType.priority for note in notifications ])
for(note in notification):
if len(note.region.all()) == 0 and (note.notificationType.priority not in notificationsPiorities ):
notifications.append(note)
notificationsPiorities.add(note.notificationType.priority)
Also, you may need to rename your variables. I can't tell for sure what are notification and notifications. One is the list you will display, and one is the list you retrieve with your query ?

Web2py - Trying to get a value from a rows object without a 'for' sentence

I'm trying to edit the group membership for a user, I have in my controller:
def change_membership():
if request.vars.id:
row = db(db.auth_membership.user_id == request.vars.id).select()
id = row[0].id
form = SQLFORM(db.auth_membership,
id,
fields=['group_id'],
_action=URL()
)
if form.process().accepted:
...redirect back to user list
if form.errors:
response.flash = 'form has errors'
return dict(form=form)
But It doesn't work, I get a :
list index out of range
I know that only get one row, but I don't understand why its seems empty..
Thanks in advance
Christian
Excellent!
Thanks a lot Anthony and Massimo.
It´s save my day, now all is working using request.get_vars.id instead of request.vars.id
Reference:
2012/8/22 Anthony wrote:
Yes. Note, web2py stores GET variables in request.get_vars and POST variables in request.post_vars. It stores both GET and POST vars in request.vars. If both get_vars and post_vars have variables with the same name, it puts their values in a list within request.vars. Just change your code to use request.get_vars.id instead of request.vars.id.
2012/8/22 Massimo Di Pierro wrote:
You have two id fields. One in request.get_vars.id (from the url) and one in request.post_vars.id (from the form submission).

Python - reading checkboxes

I have a few checkboxes with common name and individual variables (ID).
How can I in python read them as list?
Now I'm using
checkbox= request.POST["common_name"]
It isn't work properly, checkbox variable store only the last checked box instead of any list or something.
If you were using WebOB, request.POST.getall('common_name') would give you a list of all the POST variables with the name 'common_name'. See the WebOB docs for more.
But you aren't - you're using Django. See the QueryDict docs for several ways to do this - request.POST.getlist('common_name') is one way to do it.
checkbox = request.POST.getlist("common_name")
And if you want to select objects (say Contact objects) based upon the getlist list, you can do this:
selected_ids = request.POST.getlist('_selected_for_action')
object_list = Contact.objects.filter(pk__in=selected_ids)

Categories

Resources