简体   繁体   中英

Using python sessions to pass variable from one function to another function

I have a couple of quick questions - ~* when I used to code in Java, we used to reduce the usage of session variables as it used to slow the engine/occupy quite some space. In Python-django when I was trying to access one variable in two functions I have seen that request.session('variable_name') is being used to solve this - is there any other way to achieve what I wanted or request.session is the only way? In case request.session is the only approach then will sessions slow down the engine? (I apologize if its a lame question)

~* I have a list which has values that has to be saved in db table - so the list has to be iterated - model has to be instantiated - and finally it has to be saved. If the list is being iterated(say 100 times) it makes a db call 100 times to avoid that, this is what am doing with transaction.atomic():

for lcc in list_course_content:
   print lcc
   c = Course_Content(TITLE=lcc, COURSE_ID_id=crse.id)
   c.save()

am I in the right path or is there any other better approach ?

You say that you used to reduce the usage of session variables in Java, but you don't say how you did it. If it worked there, in Python it would also work.

Anyway, to be able to use variable on different requests, you have to store that variable somewhere. Language doesn't matter.In django you can set session backend, which can be based on inmemory storage, files or database, so it's your choice.

Of course you can store variable also without using sessions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM