本文整理汇总了Python中users.forms.LoginForm类的典型用法代码示例。如果您正苦于以下问题:Python LoginForm类的具体用法?Python LoginForm怎么用?Python LoginForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LoginForm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
def post(self, request):
error_messages = []
# # # Esto peta cuando cambiamos el name de l formulario
# # username = request.POST['usr']
# # password = request.POST['pwd']
# username = request.POST.get('usr')
# password = request.POST.get('pwd')
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('usr')
password = form.cleaned_data.get('pwd')
user = authenticate(username=username, password=password)
if user is None:
error_messages.append("Nombre de usuario o contraseña incorrectos")
else:
if user.is_active:
lojin(request, user)
return redirect(request.GET.get("next", 'photos_home'))
else:
error_messages.append("El usuario no está activo")
context = {
'errors': error_messages,
'login_form': form
}
return render(request, 'users/login.html', context)
示例2: logon
def logon(request):
d = {}
d["url"] = "/login"
d["sub"] = "Login"
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 HttpResponseRedirect("/")
else:
d["form"] = form
d["text"] = "Invalid login credentials"
return render(request,"register.html",d)
else:
d["form"] = form
return render(request,"register.html",d)
else:
d["form"] = LoginForm()
return render(request,"register.html",d)
示例3: login_user
def login_user(request):
"""Verify user login and password work."""
msg = []
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('users.user_page',
args=[request.user.username]))
else:
msg.append("You have entered a disabled account")
return jingo.render(request, 'users/login.html',
{'errors': msg, 'form': form})
else:
msg.append("Invalid form")
return jingo.render(request, 'users/login.html',
{'errors': msg, 'form': form})
else:
form = LoginForm()
return jingo.render(request, 'users/login.html',
{'errors': msg, 'form': form})
示例4: users_login
def users_login(request):
page_title = "Randora Login"
form = LoginForm(request.POST or None)
errors = []
context = {
"page_title": page_title,
"form": form,
"errors": errors,
}
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('profile', kwargs={'username': request.user.username}))
if request.method == 'POST':
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('profile', kwargs={'username': request.user.username}))
else:
errors.append("Your account has been disabled.")
return render(request, 'users_login.html', context)
else:
errors.append("Invalid login.")
return render(request, 'users_login.html', context)
else:
return render(request, 'users_login.html', context)
示例5: login
def login(request):
if request.POST:
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = auth.authenticate(email=email, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
#resp = {'response': 'Ok'}
resp = {'code': 0}
resp = json.dumps(resp)
return HttpResponse(resp, content_type='application/json')
else:
#resp = {'response': 'fail', 'error': u"Этот аккаунт еще не активирован"}
resp = {'code': 1, 'error': u"Этот аккаунт еще не активирован"}
resp = json.dumps(resp)
return HttpResponse(resp, content_type='application/json')
else:
resp = {'code': 2, 'error': u"Неправильный пароль или логин"}
resp = json.dumps(resp)
return HttpResponse(resp, content_type='application/json')
form = LoginForm()
return render(request, "signin.html", {'form': form})
示例6: post
def post(self, request):
error_messages = []
form = LoginForm(request.POST)
#con esto se valida que los datos cumplan con lo que se especifica en el modelo
if form.is_valid():
username = form.cleaned_data.get('usr')
password = form.cleaned_data.get('pwd')
user = authenticate(username=username, password=password)
if user is None:
error_messages.append('Nombre de usuario o contraseña incorrecta')
else:
if user.is_active:
django_login(request, user)
return redirect('posts_home')
else:
error_messages.append('El usuario no está activo')
context = {
'errors': error_messages,
'login_form': form
}
return render(request, 'users/login.html', context)
示例7: vista_login
def vista_login(request):
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('items:items'))
else:
if request.method=='POST':
form=LoginForm(request.POST)
if form.is_valid():
username=form.cleaned_data['username']
password=form.cleaned_data['password']
seguent=request.GET.get('next',default=None)
user=authenticate(username=username,password=password)
if user is not None:
if user.is_active:
login(request,user)
if bool(seguent):
return HttpResponseRedirect(seguent)
else:
return HttpResponseRedirect(reverse('items:items'))
else:
messages.error(request,'Usuari desactivat')
return HttpResponseRedirect(reverse('login'))
else:
messages.error(request,'Error, usuari i/o contrasenya són/és incorrectes/e/a')
return HttpResponseRedirect(reverse('login'))
else:
messages.error(request,'Hi ha errors en el formulari')
else:
form=LoginForm()
form.helper=FormHelper()
form.helper.form_class='form-horizontal'
form.helper.add_input(Submit('submit','Login'))
return render(request,'forms/login.html',{'form':form})
示例8: login_form
def login_form(request):
errmsg = ""
try: prevpage = smart_unicode(request.GET["next"])
except KeyError: prevpage = "/"
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(
username=form.cleaned_data["username"],
password=form.cleaned_data["password"],
)
if form.cleaned_data["prevpage"] == "login":
prevpage = "/"
else:
prevpage = form.cleaned_data["prevpage"]
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(prevpage)
else:
errmsg = "User is disabled"
else:
errmsg = "Invalid username/password"
else:
form = LoginForm(initial=dict(prevpage=prevpage))
return render_to_response("users/login.html", {
"errmsg": errmsg,
"form": form,
}, context_instance=RequestContext(request))
示例9: post
def post(self, request):
error_messages = []
form = LoginForm(request.POST)
if form.is_valid():
#forma segura de acceder a claves en un diccionario. Nunca acceder a un diccionario con la sintaxis de corchetes. POST['usr']
username = form.cleaned_data.get('usr')
password = form.cleaned_data.get('pwd')
user = authenticate(username=username, password=password)
if user is None:
error_messages.append('Nombre de usuario o contraseña incorrecto')
else:
if user.is_active:
django_login(request, user)
return redirect('photos_home')
else:
error_messages.append('El usuario no esta activo')
context = {
'errors': error_messages,
'login_form': form
}
return render(request, 'users/login.html', context)
示例10: post
def post(self, request):
error_messages = []
# Using form instade of direct request.POST
# username = request.POST.get('usr', '')
# password = request.POST.get('pwd', '')
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('usr')
password = form.cleaned_data.get('pwd')
user = authenticate(username=username, password=password)
if user is None:
error_messages.append('Nombre de usuario o clave incorrectos')
else:
if user.is_active:
django_login(request, user)
return redirect(request.GET.get('next','photos_home'))
else:
error_messages.append('El usuario no esta activo')
context = {
'errors': error_messages,
'login_form': form
}
return render(request, 'users/login.html', context)
示例11: post
def post(self, request):
error_message = []
form = LoginForm()
# paso al formulario los datos con la petición.
form = LoginForm(request.POST)
if form.is_valid():
# recupero los datos del usuario del formulario. en cleaned_data, tengo los campos normalizados.
username = form.cleaned_data.get('usr')
password = form.cleaned_data.get('pwd')
# busco el usuario, si existe, me devuelve el usuario o un nulo en caso de que no lo encuentre.
user = authenticate(username=username, password=password)
if user is None:
error_message.append('El usuario o la contraseña es incorrecta')
else:
if user.is_active:
# El usuario existe y está activo así que lo autentifico.
django_login(request, user)
# Por si he llegado al login desde una pagina que usa el decorador, me voy donde
# me indica el parametro 'next'
return redirect(request.GET.get('next', 'home'))
else:
error_message.append('El usuario no está activo')
context = {
'errors': error_message,
'login_form': form
}
return render(request, 'users/login.html', context)
示例12: hello
def hello(request, template="users/login.html"):
cc = {}
login_form = LoginForm()
registration_form = RegistrationForm()
if request.method == "POST" and "login_submit" in request.POST:
login_form = LoginForm(request.POST)
if login_form.is_valid():
user = authenticate(
username=login_form.cleaned_data["username"], password=login_form.cleaned_data["password"]
)
login(request, user)
messages.add_message(request, messages.SUCCESS, "Zostałeś zalogowany.")
if "next" in request.GET:
return HttpResponseRedirect(request.GET["next"])
else:
return HttpResponseRedirect(reverse("index"))
elif request.method == "POST" and "registration_submit" in request.POST:
registration_form = RegistrationForm(request.POST)
if registration_form.is_valid():
user_profile = registration_form.save()
user = authenticate(
username=registration_form.cleaned_data["username"],
password=registration_form.cleaned_data["password1"],
)
# send_registration_mail(request.LANGUAGE_CODE, registration_form.cleaned_data['email'], user_profile.activation_key)
return registered(request)
cc["login_form"] = login_form
cc["registration_form"] = registration_form
return render(request, template, Context(cc))
示例13: test_user_submits_invalid_data
def test_user_submits_invalid_data(self):
from users.forms import LoginForm
for email in ('[email protected]', '[email protected]'):
form = LoginForm({'email': email, 'password': 'empty'})
self.assertFalse(form.is_valid())
self.assertTrue(form.errors)
self.assertTrue(form.errors.get('email'))
示例14: index
def index(request):
if request.method == 'POST':
if 'register_form' in request.POST:
user_register = UserRegisterForm(request.POST)
if user_register.is_valid():
User.objects.create_user(username=user_register.cleaned_data['username'],
email=user_register.cleaned_data['email'],
password=user_register.cleaned_data['password'],
#tipo=user_register.cleaned_data['tipo'],
)
LogIn(request, user_register.cleaned_data['username'],
user_register.cleaned_data['password'])
return redirect('home')
if 'login_form' in request.POST:
login_form = LoginForm(request.POST)
if login_form.is_valid():
LogIn(request, login_form.cleaned_data['username'],
login_form.cleaned_data['password'])
user = User.objects.get(username = login_form.cleaned_data['username'])
#user = User.objects.filter(request.user.username == login_form.cleaned_data['username'])
return redirect('/')
user_register = UserRegisterForm()
login_form = LoginForm()
return render(request, 'index.html', {'user_register': user_register, 'login_form': login_form })
示例15: login_view
def login_view(request):
url = '/login'
sub='Login'
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
em = form.cleaned_data['username']
pwd = form.cleaned_data['password']
user = authenticate(
username=em,
password=pwd
)
if user is not None and user.is_active:
u = OjUser.objects.get(username=user.username)
if u.is_loggedin:
text = 'This user is already logged in'
return render(request,'register.html',{'sub':sub,'url':url,'form':LoginForm(),'text':text})
login(request,user)
u.is_loggedin = True
u.save()
return HttpResponseRedirect('/')
else:
text='Email and password do not match'
return render(request,"register.html",{'form':LoginForm(),'text':text,'url':url,'sub':sub})
else:
return render(request,'register.html',{'url':url,'form':form,'sub':sub})
else:
return render(request,'register.html',{'url':url,'form':LoginForm(),'sub':sub})