14

I'm very new to django and have been struggling to implement authentication for two weeks now.

When I successfully login from my /auth/login page I want to be redirected to /auth/logged_in. However, it redirects me to /auth/login/auth/logged_in instead. I can't figure out the problem. Here are the files I think youight need to help me.

settings.py

"""
Django settings for authTest project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

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

MIDDLEWARE_CLASSES = (
    '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',
)

ROOT_URLCONF = 'authTest.urls'

WSGI_APPLICATION = 'authTest.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/


STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    'auth/templates'
)

# url to redirect after successfull login
LOGIN_REDIRECT_URL = 'auth/logged_in'
LOGIN_URL='/auth/login/'

urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^auth/', include('auth.urls')),
)

auth/urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', 'auth.views.index'),
    url(r'^logged_in/$', 'auth.views.logged_in'),
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html', 'redirect_field_name': '/auth/logged_in'}),
    url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'logout.html'}),
)

auth/views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

def index(request):
  return HttpResponseRedirect('/login')

@login_required
def logged_in(request):
    return render_to_response('logged_in.html',
        context_instance=RequestContext(request)
    )

auth/templates/login.html

{% extends "base.html" %}

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
    <td>{{ form.username.label_tag }}</td>
    <td>{{ form.username }}</td>
</tr>
<tr>
    <td>{{ form.password.label_tag }}</td>
    <td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>

{% endblock %}

Thanks in advance!

gdvalderrama
  • 713
  • 1
  • 17
  • 26
DrewSSP
  • 359
  • 1
  • 2
  • 15

2 Answers2

29

Change:

LOGIN_REDIRECT_URL = 'auth/logged_in'

to:

LOGIN_REDIRECT_URL = '/auth/logged_in'

You're redirecting to a path that is appended to the current url. You need to use a leading slash to redirect to a path that is appended to the domain root.

huu
  • 7,032
  • 2
  • 34
  • 49
  • 2
    I was not aware that omitting a / would turn it into a relative path. That fixed the problem nicely. Thank you very much! – DrewSSP May 21 '14 at 00:41
  • 2
    Technically, `/auth/logged_in` is still relative. The leading slash (`/`) says to start from the root of the domain. This is the same practice in anchor tags (html) and file systems (Windows, Linux, OS X). If you're still confused, you should read a tutorial on [understanding relative paths](https://www.google.com/?q=understanding+relative+paths) to get a full grasp of this concept. – VertigoRay May 03 '16 at 21:17
  • You're correct in a very technical sense, which this being a technical Q&A site, is an appropriate distinction to make. I'll edit the answer for clarity and I appreciate your comment. – huu May 03 '16 at 21:20
0

make changes in settings.py file as

LOGIN_REDIRECT_URL = '/auth/logged_in/'
gofr1
  • 15,741
  • 11
  • 42
  • 52
raghu
  • 384
  • 7
  • 10