How do you pass or share variables between django views?

不羁岁月 提交于 2019-12-17 16:28:05

问题


I'm kind of lost as to how to do this:

I have some chained select boxes, with one select box per view. Each choice should be saved so that a query is built up. At the end, the query should be run.

But how do you share state in django? I can pass from view to template, but not template to view and not view to view. Or I'm truly not sure how to do this. Please help!


回答1:


There are many ways... in the view to template... put the variables in hidden fields in the forms. So when you "submit" in the subsequent forms... the values are then contained in the following request.POST.get().

Of course you can also store the various data elements in a DB table (disk or ram) between views... using the session_id as the key into the datastore. (not recommended for load balanced systems).

And my least favorite is cookies. (see the APIs for how to store in cookies)

UPDATE: Sorry there are no code examples here... the docs are pretty easy to read. There is also a djangosnippets website that you use to look up example code.




回答2:


You can store such information in session as Ignacio Vazquez-Abrams said or use django-flash - (django-flash usage)




回答3:


Put the values to hold into the session.



来源:https://stackoverflow.com/questions/2570131/how-do-you-pass-or-share-variables-between-django-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!