我觉得这是一个简单的问题,我只是错过了一小步。
我要执行以下任意数量的操作(作为下一个参数中的术语):
[not signed in] -> profile -> login?next=/accounts/profile/ -> auth -> profile.
[not signed in] -> newsfeed -> login?next=/newsfeed/` -> auth -> newsfeed.
鉴于我现在要去:
[not signed in] -> profile -> login?next=/accounts/profile/ -> auth -> loggedin
[not signed in] -> newsfeed -> login?next=/newsfeed/ -> auth -> loggedin
我希望以某种方式将next
参数从login
上的表单传递到auth
,并将auth
重定向到此参数
目前我正在尝试login.html
:
<input type='text' name="next" value="{{ next }}">
然而,这并没有得到下一个值。从调试工具栏可以看到:
GET data
Variable Value
u'next' [u'/accounts/profile/']
views
:
def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
print request.POST
return HttpResponseRedirect(request.POST.get('next'),'/accounts/loggedin')
else:
return HttpResponseRedirect('/accounts/invalid')
login.html
:
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p class="error"> Sorry, you have entered an incorrect username or password</p>
{% endif %}
<form action="/accounts/auth/" method="post">{% csrf_token %}
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type='text' name="next" value="{{ request.GET.next }}">
<input type="submit" value="login">
</form>
{% endblock %}
settings
:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^admin/', include(admin.site.urls)),
('^accounts/', include('userprofile.urls')),
url(r'^accounts/login/$', 'django_yunite.views.login'),
url(r'^accounts/auth/$', 'django_yunite.views.auth_view'),
url(r'^accounts/logout/$', 'django_yunite.views.logout'),
url(r'^accounts/loggedin/$', 'django_yunite.views.loggedin'),
url(r'^accounts/invalid/$', 'django_yunite.views.invalid_login'),
)
settings
:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# 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',
'debug_toolbar',
'userprofile',
)
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 = 'django_yunite.urls'
WSGI_APPLICATION = 'django_yunite.wsgi.application'
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-ca'
TIME_ZONE = 'EST'
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/'
STATICFILES_DIRS = (
('assets', '/home/user/GitHub/venv_yunite/django_yunite/static/'),
)
TEMPLATE_DIRS = (
'./templates',
'/article/templates',
)
STATIC_ROOT = "/home/user/Documents/static/"
AUTH_PROFILE_MODULE = 'userprofile.UserProfile'
打印语句显示空的u'next'
以前也遇到过类似的情况。为了解决这个问题我写了我自己的装饰-
上面的decorator检查用户身份验证。如果用户未通过身份验证,则通过从请求对象传递
url
将用户重定向到登录页。查询字符串被隐式地传递给任何视图,而无需编写任何特殊代码。
您只需确保将
next
键从实际登录表单(在您的情况下,这是在/accounts/login/
中呈现的表单)传递到/accounts/auth
视图。为此,需要确保在设置中启用了请求模板上下文处理器(
django.core.context_processors.request
)。为此,首先需要导入TEMPLATE_CONTEXT_PROCESSORS
的默认值,然后将请求处理器添加到settings.py
中,如下所示:然后在表格中:
现在,在您的视图中:
你要找的是一个login decorator。
在您的视图中
然后在urls.py中添加验证url
最后:确保已在安装的应用程序中安装了django.contrib.auth,并安装了AuthenticationMiddleware。
设置.py
您的模板/registration/login.html
相关问题 更多 >
编程相关推荐