本文整理汇总了Python中django.contrib.auth.forms.AuthenticationForm.add_error方法的典型用法代码示例。如果您正苦于以下问题:Python AuthenticationForm.add_error方法的具体用法?Python AuthenticationForm.add_error怎么用?Python AuthenticationForm.add_error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.forms.AuthenticationForm
的用法示例。
在下文中一共展示了AuthenticationForm.add_error方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request_login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import add_error [as 别名]
def request_login(request):
if request.POST:
print "attempting login"
form = AuthenticationForm(data=request.POST)
if not form.is_valid():
print "auth form is not valid"
form.add_error(None, "Login failed")
return render(request, "wishlist_app/login.html", {"login_form": form})
"auth form is valid"
username = request.POST['username']
password = request.POST['password']
print "authenticating %s" % username
user = authenticate(username=username, password=password)
login(request, user)
return redirect("wishlists")
else:
form = AuthenticationForm()
return render(request, "wishlist_app/login.html", {"login_form": form})
示例2: index
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import add_error [as 别名]
def index(request):
# are they logged in already? on to the incidents.
if request.method == 'GET' and request.user.is_authenticated() and request.user.is_active:
return redirect('/incidents')
login_form = AuthenticationForm(data=request.POST or request.GET or None)
if login_form.is_valid():
username, password = login_form.cleaned_data['username'], login_form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
last_login = user.last_login
login(request, user)
if not last_login:
return redirect('change_password')
else:
return redirect('incident_list')
else:
login_form.add_error(None, "Your user is inactive.")
# note -- may be returned in both GET and POST requests
# (POST if their user/pass is invalid or inactive)
return render(request, 'login.html', {'login_form': login_form})