Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect after login simply appends LOGIN_REDIRECT_URL

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!

like image 299
DrewSSP Avatar asked May 21 '14 00:05

DrewSSP


1 Answers

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.

like image 178
huu Avatar answered Nov 15 '22 19:11

huu