本文整理汇总了Python中forms.LoginForm.add_error方法的典型用法代码示例。如果您正苦于以下问题:Python LoginForm.add_error方法的具体用法?Python LoginForm.add_error怎么用?Python LoginForm.add_error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.LoginForm
的用法示例。
在下文中一共展示了LoginForm.add_error方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user_login
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import add_error [as 别名]
def user_login(request):
# data sent to template
data = {}
# redirect url
redir = "/"
# if user is already authenticated, redirect to home
if request.user.is_authenticated():
return HttpResponseRedirect(redir)
# if post method, try to login
if request.method == "POST":
form = LoginForm(request.POST)
data["form"] = form
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
# authenticate
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(redir)
else:
form.add_error(None, "Account disabled. Contact administrator.")
else:
form.add_error(None, "Invalid username or password.")
# if no post method, create empty login form
else:
data["form"] = LoginForm()
return render(request, 'login.html', data);
示例2: register
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import add_error [as 别名]
def register(request):
if request.method == 'POST':
a = request.POST.get('action')
if a == 'register':
register_f = RegisterForm(request.POST)
login_f = LoginForm()
if register_f.is_valid():
try:
User.objects.create_user(
register_f.cleaned_data['username'],
register_f.cleaned_data['email'],
register_f.cleaned_data['password'])
except IntegrityError, e:
if 'username' in str(e):
register_f.add_error('username',
'Username is taken')
elif 'email' in str(e):
register_f.add_error('email',
'Email is already registered')
else:
raise e
else:
user = authenticate(
username=register_f.cleaned_data['username'],
password=register_f.cleaned_data['password'])
login(request, user)
Timezone.objects.create(user=user)
project = Project()
project.user = user
project.name = 'default project'
project.description = 'default project description'
project.color = Project.DEFAULT_COLOR
project.save()
return redirect('dashboard')
elif a == 'login':
register_f = RegisterForm()
login_f = LoginForm(request.POST)
if login_f.is_valid():
user = authenticate(
username=login_f.cleaned_data['username'],
password=login_f.cleaned_data['password'])
if user is None:
login_f.add_error('username',
'Wrong credentials')
else:
login(request, user)
return redirect('dashboard')
else:
register_f = RegisterForm()
login_f = LoginForm()
示例3: logining
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import add_error [as 别名]
def logining(request):
if (request.method == "GET"):
loginForm = LoginForm()
else:
loginForm = LoginForm(request.POST)
if loginForm.is_valid():
user = authenticate(username=loginForm.data['username'], password=loginForm.data['password'])
if (user is not None):
session = do_login(request,user)
response = HttpResponseRedirect('/')
response.set_cookie('sessionid', session.session_key, domain='.localhost', httponly=True, expires = session.expire_date)
return response
else:
loginForm.add_error(filed=None, error="wrong login/pass")
return render(request,'login.html',{
"form" : loginForm,
})
示例4: login
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import add_error [as 别名]
def login(request):
cxt = {}
cxt.update(csrf(request))
if request.method == 'POST':
login_form = LoginForm(request.POST)
next_page = request.POST.get('next', '/home/')
if login_form.is_valid():
data = login_form.cleaned_data
username = data['username']
password = data['password']
user = auth.authenticate(username=username, password=password)
if user and user.is_active:
auth.login(request, user)
return redirect('%s' % next_page)
else:
login_form.add_error('username', 'Invalid user or password')
else:
login_form = LoginForm()
next_page = request.GET.get('next', '/home/')
cxt.update({'login_form': login_form, 'next': next_page})
return render_to_response('registration/login.html', cxt)