Search code examples
djangodjango-viewsdjango-context

Django 1.10 - Maintaining context object information


My question is, is there a way to maintain the context object over multiple views? Say you are at the home page, you click a link to go to a specific part of the application but you want to maintain the context information as to not excessively query the DB for this information every time you change views.

I know of mixins and such, but these do not seem to maintain the information, or am I wrong? Question is, can I access the context information and pass it to the next view?


Solution

  • In order to maintain state across views, you can use django sessions.

    The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis.

    You have to enable the session middleware and add the appropriate settings. Once you do, you will have to manually add code to each view to check to see if the session has information related to that view’s context and to update the session with the data from the context.