How to Destroy a Session Variable in Django



Python


In this article, we show how to destroy a session variable in Django.

A session variable is useful because it retains its value across multiple web pages. So even if a user clicks on an anchor tag and goes to another page on your site, the value of that session variable remains. This is the importance of session variables.

At a certain point during a user session, however, you may want to terminate the session and destroy (or delete) session variables.

So we show how to do this in this article.

So in order to destroy a session variable in Django, we use the following code shown below.



So in order to delete a session variable, we use the term, del request.session[]

Inside of this request.session[] variable, we place the name of the session variable. In the above case, it is 'username'.

Now this session variable is destroyed.

And this is how to destroy (or delete) a session variable in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...