0

I'm using Django 1.9 and following along on this tutorial: https://www.youtube.com/watch?v=6WkQOlYgkHM&index=15&list=PLEsfXFp6DpzQFqfCur9CJ4QnKQTVXUsRy

When I put in the following code as recommended:

def post_list(request):
    # return HttpResponse("<h1>Update</h1>")

    if request.user.is_authenticated():
        context = {
        "title": "My User List"
        }
    else:
        context = {
            "title": "List"
        }
    return render(request, "index.html", context)

I get the following error message:

AttributeError: 'WSGIRequest' object has no attribute 'user'

I looked up the documentation and tried doing this:

from django.contrib.auth.models import user

But user is underlined on my pycharm and now the error message I'm getting is

ImportError: cannot import name 'user'
fred russell
  • 307
  • 2
  • 11

1 Answers1

1

You probably forgot to add some required middleware items to your MIDDLEWARE setting.

As explained in the documentation for User authentication in Django, you must add SessionMiddleware and AuthenticationMiddleware to the MIDDLEWARE setting in your settings.py file. That will make sure the user attribute is set on your request objects.

Note that the above is true for the current state of Django (1.10 and later), Django 1.9 is not supported anymore and should not be used.

If you still want to use it, instructions for using django.contrib.auth with django 1.9 can be found here

Jieter
  • 4,101
  • 1
  • 19
  • 31
  • Thanks @Jieter but I'm pretty sure I have those: `MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',` – fred russell Dec 28 '17 at 17:18
  • Also, how do you make a line break in the comment section? If I hit return then that ends my comment. – fred russell Dec 28 '17 at 17:20
  • @fredrussell, you need to change MIDDLEWARE to MIDDLEWARE_CLASSES. Check links in comments by @neverwalkaloner and @aashu. And importing `user` doesn't work, because model is called `User`. – Borut Dec 28 '17 at 17:26
  • Thanks, that did it. – fred russell Dec 28 '17 at 17:41
  • I'd suggest not changing `MIDDLEWARE` to `MIDDLEWARE_CLASSES`, but rather upgrade to a supported version of Django (1.11 or 2.0). Especially when you are learning, there is no point in using an old version, apart from the specific tutorial you are working from. – Jieter Dec 29 '17 at 10:37
  • yes, there is. I tried using the most updated version while following along in this tutorial which uses 1.9 and I confronted a lot of bugs because I was using a 1.11 version on a 1.9 tutorial. And since I am new I have to call my personal tutor in order to remove the bugs. Switching back to 1.9 has made things a lot easier. – fred russell Dec 31 '17 at 22:39