登录后重定向只需添加LOGIN_REDIRECT_URL [英] Redirect after login simply appends LOGIN_REDIRECT_URL
问题描述
我对django并不陌生,并且一直在努力实施身份验证两周了.
I'm very new to django and have been struggling to implement authentication for two weeks now.
当我从/auth/login
页面成功登录时,我想重定向到/auth/logged_in
.但是,它会将我重定向到/auth/login/auth/logged_in
.我不知道问题所在.这是我认为您需要帮助的文件.
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.
"""
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
django.conf.urls中的
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
django.conf.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
从django.http中的
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 %}
提前谢谢!
推荐答案
更改:
LOGIN_REDIRECT_URL = 'auth/logged_in'
收件人:
LOGIN_REDIRECT_URL = '/auth/logged_in'
您将重定向到附加到当前URL的路径.您需要使用斜杠将其重定向到附加到域根目录的路径.
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.
这篇关于登录后重定向只需添加LOGIN_REDIRECT_URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!