0
votes

I'm trying to authenticate user in Django using login() and authenticate() methods. Once it is done, I redirect to authenticated landing page. And when I try to access request.user object I always get anonymous use object even though I'm authenticating user normally. Any idea why? Here's the code for login.FYI, I'm using Django registration. And users are created (along with their usernames & passwords) in DB.

def login_my_user(request):
    name = request.POST.get('username', '')
    pwd = request.POST.get('password', '')
    user = authenticate(username=name, password=pwd)
    if user is not None:
        if user.is_active:
            login(request, user)
            #redirect to authenticated page

Settings.py file information

# Middleware Settings

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)


# apps

INSTALLED_APPS = (
    'django.contrib.contenttypes',
    'django.contrib.staticfiles',
    'django.contrib.sessions',
    'django.contrib.auth',
    'test'
)


#Template
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (
    PROJECT_DIR + "/templates",
)
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
)

Checking user credentials for some dashboard for users

def user_dashboard(request):
    user = request.user
    print user

Now here it gives AnonymousUser issue

1
Please update the question with your settings.pyBurhan Khalid
Added, information. Rest other is DB settings or logging info, which I don't think is useful hereWiz
Can you post the code where you check for authenticated user?Burhan Khalid
This could be a session/cookie issueBurhan Khalid

1 Answers

0
votes

what the AUTHENTICATION_BACKENDS you use?

If you don`t know, set the option in your settings.py:

AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)