我需要在登录时比较一个布尔变量(变量名称:aproved) 如果变量为true:登录成功;否则登录错误; 我有变量存储在数据库中。
我的表格:
class LoginForm(AuthenticationForm):
username (...)
password (...)
的url:
`url(r'^login/$', auth_views.login, {'authentication_form': LoginForm},` `name='login'),`
查看:
def login(request):
return render(request, 'registration/login.html')
如何更改默认登录以允许此操作?
答案 0 :(得分:0)
根据特定权限限制访问权限
这是一个非常好的方法来检查是否有一些"测试"通过。在你的例子中,"测试"如果approved
变量为True
,则为{}}。你可以这样做:
from django.contrib.auth.decorators import user_passes_test
def check_approved():
# get the approved variable from database
approved = True # or false, depends on what you read from the database
return approved
@user_passes_test(check_approved)
def login(request):
...
因此,@user_passes_test()
装饰器会收到一个必须返回True
或False
的回调。您可以在the docs中了解有关它的更多信息。
如果这对您没有帮助,请详细说明您想要做什么以及为什么这种方法无法为您提供帮助。