Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User has no attribute session -> Django

Querying my database to get a user so I can log them out, but getting the above error.

def logout(request):
    id = request.session["user_id"]
    user = get_object_or_404(User, pk=id)
    auth.logout(user)

I'm not trying to to say that User has a session attribute, I telling it that it's primary key is equal to the number held in the session.

like image 814
Chris Avatar asked Dec 07 '11 05:12

Chris


People also ask

How to apply session in Django?

If you want to use a database-backed session, you need to add 'django. contrib. sessions' to your INSTALLED_APPS setting. Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.

How sessions work in Django?

Sessions are the mechanism used by Django (and most of the Internet) for keeping track of the "state" between the site and a particular browser. Sessions allow you to store arbitrary data per browser, and have this data available to the site whenever the browser connects.

How to clear session in Django?

As of Django 1.8, any call to flush() will log out the user. From the docs: Changed in Django 1.8: Deletion of the session cookie is a behavior new in Django 1.8. Previously, the behavior was to regenerate the session key value that was sent back to the user in the cookie.


1 Answers

auth.logout() expects request not a user instance. Assuming you're talking about django.contrib.auth

https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.logout

like image 184
Yuji 'Tomita' Tomita Avatar answered Sep 28 '22 09:09

Yuji 'Tomita' Tomita