- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于 django 的新问题(这些天我发布了一个丢失的问题^^)。
这是我的情况:我有一个自定义登录 View (在设置中注册为登录 url),我在其中对用户进行身份验证。我选择做一个自定义 View ,以便能够添加消息和日志记录。
身份验证工作正常,但我对 GET 参数“next”有疑问。它由重定向用户进行身份验证的 View 自动设置。在我看来,它用于在成功登录后重定向用户。
代码如下:
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.template import RequestContext
from django.contrib.auth import authenticate, login, logout
import logging
logger = logging.getLogger("views")
def login_user(request):
"""
Displays the login form or authenticates the user if called by POST.
"""
accepted = False
next = request.GET.get('next', None)
if not request.user.is_authenticated():
if request.POST:
# Get the form data and check the user credentials
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
# Log the user in
login(request, user)
logger.info("Login of user : %s", user.username)
# Confirm the login
messages.success(request,_('Login successful. Welcome !'))
accepted = True
else:
messages.error(request,_('This account has been disabled by the administrator.'))
else:
messages.warning(request,_('The given username or password is invalid. Please try again.'))
else:
# If already authenticated
accepted = True
# Choose where to go
if accepted:
return HttpResponse(next)
if next:
return HttpResponseRedirect(next)
else:
return HttpResponseRedirect(reverse('myview'))
else:
return render_to_response('login.html',
context_instance=RequestContext(request))
当用户在已经通过身份验证(首先是其他)的情况下访问登录 View 时,下一个参数是正确的。
当匿名用户尝试访问/editor/25(例如)并被重定向到登录以进行身份验证时,即使出现在 url 中,“next”也始终为 None(它应该是“/editor/25”) .
某处应该有一个简单的错误。也许它与 authenticate() 或 login() (django.contrib.auth) 有关。
感谢您的帮助。
最佳答案
When an anonymous user tries to go to /editor/25 (for example) and is redirected to login for authentication, "next" is always None even if present in the url (it should be "/editor/25").
这听起来很奇怪。您能否检查该 URL 是否确实将 ?next=/editor/25/
作为查询字符串?同时记录 request.GET
并查看结果。
此外,您可能希望从 request.GET
中获取 next
参数,并在呈现模板时将其作为(可选)隐藏输入包含在表单中。 auth 模块的 login
View 执行此操作。这样,您的表单就可以在 POST
上从 request.POST
中获取 next
。
关于python - Django 1.2 : login issue (GET parameter: next),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861840/
目前我正在尝试创建一个 Web 部署包。所以我在我的项目的根目录中添加了一个 parameters.xml 并指定了一些自定义参数。 我发现我的很多参数都部分相同。所以我想做某种参数引用。寻找这个,我
如何设置我的 Symfony 2 项目以使用 parameters.yml 而不是 parameters.ini? 在 Controller 中,我可以像这样从 parameters.ini 中获取变
有什么建议说明为什么此 AWS CloudFormation 不断回滚吗? { "Description" : "Single Instance", "Resources" : {
PARAMETERS: p_1 TYPE i, p_2 TYPE i. 因此在初始屏幕中,我看到了 2 个文本框,每个参数一个。 如果我填写其中一个,但不按回车键,然后我在第二个上调用 F4 帮助,我
我需要存储 Parameter由 Build() 返回作为 Parameter (因为我将参数存储在一个数组中,另一种方法就是为每个参数数量复制粘贴相同的类太多,因为 c# 没有可变参数泛型)。 问题
我正在为我的 CS 类(class)做作业,它使用了 const,我对何时使用它们感到有点困惑。 这3个函数有什么区别? int const function(parameters) int fu
在 xgboost 的文档中,我读到: base_score [default=0.5] : the initial prediction score of all instances, global
我正在创建一个新的 REST 服务。 向 REST 服务传递参数的标准是什么。在 Java 的不同 REST 实现中,您可以将参数配置为路径的一部分或请求参数。例如, 路径参数 http://www.
在我的程序中,我需要验证传递给程序的参数是一个整数,所以我创建了这个小函数来处理用户键入“1st”而不是“1”的情况。 问题是它根本不起作用。我尝试调试,我只能告诉你参数是 12,long 是 2。(
谁能告诉我如何使用存储在 &rest 指定值中的参数。 我已经阅读了很多,似乎作者只知道如何列出所有参数。 (defun test (a &rest b) b) 这很高兴看到,但并不是很有用。 到目前
我使用 git 有一段时间了,但大多数时候我更喜欢与 Intelij IDEA 的集成。现在,为了扩展我对系统的知识和理解,我决定更多地使用命令行。我观察到的是有两种类型的参数: --paramete
我正在用 RAML 编写一些 REST 文档,但我被卡住了。 我的问题: - 我有一个用于搜索的 GET 请求,它可以采用参数“id”或( 独占或 )“引用”。拥有 只有其中之一 是必须的。 我知道怎
我定义了一个这样的 Action : /secure/listaAnnunci.action /login.jsp 我可以从 Action 内部访问参数吗?谢谢 最佳答案 您需要实现 S
我有一个 TeamCity 8.0.3 项目,其中包含多个配置,其中有一个通用参数(定义为项目参数):targetServerIP .这些配置之一是“一键部署”,它通过使用快照依赖项启动其他配置。我已
try{ Class.forName("com.mysql.jdbc.Driver"); mycon = DriverManager.getConnec
我在实际的 javascript 项目中遇到了一个非常奇怪的情况。我创建了一个自定义事件并将数组放入该事件 $.publish("merilon/TablesLoaded", getTables())
在使用参数数组进行插入/更新期间,可以忽略一个/一些特定行的一个/一些参数。 我提供了一个简单的例子。想象一下,我们有一个包含 3 列的表:X、Y 和 Z。我们想在 block 中执行更新(如果缺少某
如何编写接受未定义参数的函数?我想它可以像那样工作: void foo(void undefined_param) { if(typeof(undefined_param) == int) {
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章PDO版本问题 Invalid parameter number: no
Jenkins 管道作业如下所示: 部分 Jenkinsfile(我们使用脚本化管道)是: properties([parameters([string(defaultValue: "", descr
我是一名优秀的程序员,十分优秀!