gpt4 book ai didi

python - 登录后重定向只需附加 LOGIN_REDIRECT_URL

转载 作者:太空狗 更新时间:2023-10-29 21:24:21 25 4
gpt4 key购买 nike

我是 django 的新手,两周以来一直在努力实现身份验证。

当我从我的 /auth/login 页面成功登录时,我想被重定向到 /auth/logged_in。但是,它会将我重定向到 /auth/login/auth/logged_in。我无法弄清楚问题所在。以下是我认为您需要帮助我的文件。

设置.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 %}

提前致谢!

最佳答案

改变:

LOGIN_REDIRECT_URL = 'auth/logged_in'

到:

LOGIN_REDIRECT_URL = '/auth/logged_in'

您正在重定向到附加到当前 url 的路径。您需要使用前导斜杠重定向到附加到域根的路径。

关于python - 登录后重定向只需附加 LOGIN_REDIRECT_URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23772001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com