本文整理汇总了Python中accounts.forms.LoginForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python LoginForm.is_valid方法的具体用法?Python LoginForm.is_valid怎么用?Python LoginForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类accounts.forms.LoginForm
的用法示例。
在下文中一共展示了LoginForm.is_valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_login_form_validation_for_blank_items
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def test_login_form_validation_for_blank_items(self):
email_blank_form = LoginForm(
data={'email': '', 'password': self.valid_password}
)
self.assertFalse(email_blank_form.is_valid())
password_blank_form = LoginForm(
data={'email': self.valid_email, 'password': ''}
)
self.assertFalse(password_blank_form.is_valid())
示例2: login
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def login(request):
if request.user.is_authenticated():
return redirect(reverse('home'))
_request = None
if request.method == 'POST':
_request = request.POST.copy()
else:
_request = request.GET.copy()
next_uri = _request.get('next', get_config('LOGIN_REDIRECT_URL',
reverse('home')))
# rescuing poor users from infinite redirection loop
if next_uri == get_config('LOGIN_URL', reverse('login')):
next_uri = get_config('LOGIN_REDIRECT_URL', reverse('home'))
login_form = LoginForm()
if request.method == 'POST':
login_form = LoginForm(request.POST)
if login_form.is_valid() and login_form.user:
auth_login(request, login_form.user)
messages.success(request, "Hello, {0}.".format(login_form.user))
badges.possibly_award_badge("logged_in",
user=login_form.user)
return redirect(next_uri)
return {
'login_form': login_form,
'next': next_uri,
}
示例3: login
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def login(request):
"""Login. Looks up by username or email address."""
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
next_url = None
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
user = auth.authenticate(username=request.POST['username'],
password=request.POST['password'])
if not user:
try:
email_user = User.objects.get(
email__exact=request.POST['username'])
user = auth.authenticate(username=email_user.username,
password=request.POST['password'])
except User.DoesNotExist:
user = None
if user:
if user.is_active:
auth.login(request, user)
if 'next' in request.POST:
return HttpResponseRedirect(request.POST['next'])
else:
return HttpResponseRedirect(reverse('index'))
else:
form.errors['username'] = 'This user is not active yet, please try later.'
else:
form.errors['username'] = (
'username or email address not found and/or password wrong')
else:
form = LoginForm()
next_url = urllib.unquote_plus(request.GET.get('next', '')).decode('utf8')
return render(request, 'accounts/login.html', {'login_form': form,
'next': next_url})
示例4: mylogin
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def mylogin(request):
form = LoginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
try :
the_user = MyUser.objects.get(username=username)
user = authenticate(username=the_user.username,password=password)
except :
user = None
if user is not None:
# user = authenticate(username=the_user.username,password=password)
if user.is_active:
login(request,user)
messages.success(request, 'Vous êtes maintenant connecté')
else:
messages.success(request, "Votre compte est en attente d'activation")
pass
else:
return redirect('/register/')
context = {'form':form}
return render(request,"registration/login.html",context)
示例5: view_account
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def view_account(request):
if request.method != 'POST':
return HttpResponse(json.dumps({'success': False, 'message': 'POST requests only'}), mimetype='application/json')
form = LoginForm(request.POST)
if not form.is_valid():
return HttpResponse(json.dumps({'success': False, 'message': form.errors}), mimetype='application/json')
email = form.cleaned_data.get('email', None)
password = form.cleaned_data.get('password', None)
create = form.cleaned_data.get('create', False)
if email is None or password is None:
return HttpResponse(json.dumps({'success': False, 'message': 'Missing required parameters email or password'}), mimetype='application/json')
user = accounts.user_for_email(email)
success_message = 'Login Successful'
if user is None and create is True:
success_message = 'Account Creation Successful'
user = PushUser.objects.create_user(username=email, email=email, password=password)
user = authenticate(username=email, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse(json.dumps({'success': True, 'message': success_message}), mimetype='application/json')
return HttpResponse(json.dumps({'success': False, 'message': 'Invalid Login'}), mimetype='application/json')
示例6: login
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def login(request):
login_form = LoginForm()
register_form = RegistrationForm()
next = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
# Special case next urls that will lead us into redirect loops
if next == settings.LOGIN_URL:
next = settings.LOGIN_REDIRECT_URL
if 'kind' in request.POST:
if request.POST['kind'] == 'register':
register_form = RegistrationForm(request.POST)
if register_form.is_valid():
_backend_hackend(request, register_form.save())
return HttpResponseRedirect(next)
elif request.POST['kind'] == 'login':
login_form = LoginForm(request.POST)
if login_form.is_valid() and login_form.user:
_backend_hackend(request, login_form.user)
return HttpResponseRedirect(next)
context = {
'login_form': login_form,
'register_form': register_form,
'next': next,
}
return render_to_response('accounts/login.html', context,
context_instance=RequestContext(request))
示例7: login_request
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def login_request(request):
if request.user.is_authenticated():
return reverse(index)
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect(reverse('create'))
else:
# Invalid User or Pass
messages.add_message(request, ERROR, 'You have provided invalid login information.')
return render(request, 'login.html', {
'form':form
})
else:
# Blank
messages.add_message(request, ERROR, 'Please enter correct login information.')
return render(request, 'login.html', {
'form':form
})
else:
# No POST, Show Form
messages.add_message(request, INFO, 'Please login below.')
return render(request, 'login.html', {
'form':LoginForm(),
})
示例8: signin
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def signin(request):
error = []
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method =="POST":
form = LoginForm(request.POST) #pass the form the request.post values
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
userprofile = authenticate(username=username, password=password) #if passwords are valid
if userprofile is not None: #if all went well
if userprofile.is_active:
login(request, userprofile) #login the request as this particular user
return HttpResponseRedirect('/a/profile/')
else:
errors.append('User %s is disabled' % userprofile.username)
else:
error.append('Username and password combination did not match.')
return render_to_response('accounts/login.html', {'form':form, 'error':error}, context_instance=RequestContext(request)) #wrong credentials, go back and look at errors
else:
return render_to_response('accounts/login.html', {'form':form}, context_instance=RequestContext(request))
else:
""" nothing has been done, show the form"""
form = LoginForm()
context = {'form': form, 'errors':error,}
return render_to_response('accounts/login.html', context, context_instance=RequestContext(request))
示例9: test_wrong_username
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def test_wrong_username(self):
data = self.data
data['username'] = 'toastclient'
form = LoginForm(data)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['__all__'],
["Invalid username and/or password"])
示例10: loginview
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def loginview(request):
if request.method == 'POST':
form=LoginForm(request.POST)
session_cart=getCart(request)
try:
next= request.POST['next']
except:
next='/'
if form.is_valid():
username=form.cleaned_data['username']
password=form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
user_cart=getCart(request)
cartMerge(user_cart, session_cart)
request.session['cart']={}
return HttpResponseRedirect(next)
else:
if '_auth_user_id' in request.session:
return HttpResponseRedirect('/')
else:
form=LoginForm()
try:
next= request.GET['next']
except:
next='/'
return render_to_response('login_form.html',
{'form': form,
'next': next},
context_instance=RequestContext(request))
示例11: login
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def login(request):
template = 'accounts/login.html'
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('accounts:index'))
else:
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = request.POST['login']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return HttpResponseRedirect(reverse('saisie:index'))
else:
return render(request, template, {
'form': form,
'error': 'La connexion a échouée.',
})
else:
return render(request, template, {
'form': form,
'error': 'Veuillez remplir tous les champs correctement.',
})
else:
form = LoginForm()
return render(request, template, {
'form': form,
})
示例12: test_empty_password
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def test_empty_password(self):
form = LoginForm({'username': 'test', 'password': ''})
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors,
{'password': [u'This field is required.']}
)
示例13: LoginView
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
class LoginView(TemplateView):
template_name = "signin.html"
def __init__(self):
super(LoginView, self).__init__()
self.form = LoginForm()
def get_context(self, request, *args, **kwargs):
context = {
'form': self.form
}
return context
def get(self, request, *args, **kwargs):
return self.render_to_response(self.get_context(request))
def post(self, request, *args, **kwargs):
self.form = LoginForm(request.POST)
if self.form.is_valid():
self.form.save()
user = auth.authenticate(
email=self.form.user.email,
password=self.form.str_password
)
auth.login(request, user)
del request.POST['email']
del request.POST['password']
return redirect('index_page')
else:
return self.render_to_response(self.get_context(request))
示例14: test_too_long_username
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def test_too_long_username(self):
form = LoginForm(self.too_long_username)
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors,
{'username': [u'Ensure this value has at most 64' +
' characters (it has 65).']}
)
示例15: login
# 需要导入模块: from accounts.forms import LoginForm [as 别名]
# 或者: from accounts.forms.LoginForm import is_valid [as 别名]
def login(request, template_name='signin.html'):
if request.GET.get('next', None):
request.session['POST_AUTH_URL'] = request.GET.get('next')
if 'POST' == request.method:
form = LoginForm(data=request.POST)
if form.is_valid():
auth.login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
if form.cleaned_data['keep_signed_in']:
# Sessions expire in about ten years.
request.session.set_expiry(datetime.timedelta(3650))
else:
# Session expires when user closes browser.
request.session.set_expiry(0)
if request.GET.get('ajax'):
pass
'''
from utils import json
return HttpResponse(
json.get_json(
request,
200,
ugettext(u'Login successful.'),
dict()
),
content_type=json.get_response_mimetype(request)
)
'''
else:
return accounts.post_auth_redirect(request)
else:
if request.GET.get('ajax'):
pass
'''
from utils import json
return HttpResponse(
json.get_json(
request,
403,
ugettext(u'Authentication Failed. Access forbidden.'),
dict()
),
content_type=json.get_response_mimetype(request)
)
'''
else:
form = LoginForm()
context = dict(
form=form
)
return render_to_response(
template_name,
context,
context_instance=RequestContext(request)
)