我的应用程序有一个登录选项,我正在尝试当用户汇总登录表单时,此验证,如果不对,说用户和密码不存在,但没有我的主页面重新加载或重定向到另一个页面,登录表单位于同一索引页面
view.py
@ajax_request
def index(request):
notifi = Notificaciones.objects.filter(user=request.user.id, Estado=False)
if request.method == 'POST':
formlog = LoginUserForm(request.POST, request.FILES)
if formlog.is_valid():
cleaned_data = formlog.clean_data
username = cleaned_data.get('username')
password = cleaned_data.get('password')
user = authenticate(nombre_de_usuario=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return {"status" : "true"}
else:
return {"status" : "false", "reason" : "You need to activate your account. Please check your email"}
else:
return {"status" : "false", "reason" : "Invalid username/password"}
else:
formlog = LoginUserForm()
else:
formlog = LoginUserForm()
if request.method == 'POST':
form = RegistroUserForm(request.POST, request.FILES)
if form.is_valid():
cleaned_data = form.cleaned_data
username = cleaned_data.get('username')
email = cleaned_data.get('email')
password = cleaned_data.get('password')
user_model = MiUsuario.objects.create_user(nombre_de_usuario=username, email=email, password=password)
user_model.save()
else:
form = RegistroUserForm()
else:
form = RegistroUserForm()
context = {
'formlog' : formlog,
'form': form,
'notifi': notifi,
'year' : datetime.now().year,
}
return render(request,'app/index.html',context)
我正在使用视图中的Ajax
form.py
class LoginUserForm(AuthenticationForm):
"""Authentication form which uses boostrap CSS."""
username = forms.CharField(label=_(""),max_length=254,widget=forms.TextInput({'class': 'form-control','placeholder': 'Nombre de usuario'}))
password = forms.CharField(label=_(""),widget=forms.PasswordInput({'class': 'form-control','placeholder':'Contraseña'}))
def clean_username(self):
username = self.cleaned_data['username']
if MiUsuario.objects.filter(nombre_de_usuario!=username):
raise forms.ValidationError('Nombre de usuario no existe.')
return username
def clean_password(self):
password = self.cleaned_data['password']
if MiUsuario.objects.filter(password!=password):
raise forms.ValidationError('Contraseña incorrecta.')
return password
的login.html
<form method="post" action="/login" enctype="multipart/form-data" class="form-horizontal">
{% csrf_token %}
<div class="col-md-12 no-padding">
{{ formlog.username }}
<div class="text-danger">
{%if form.errors %}
<p class="validation-summary-errors">{{forms.ValidationError}}</p>
{% endif %}
</div>
</div>
<div class="col-md-12 no-padding">
{{ formlog.password }}
<div class="text-danger">
{%if form.errors %}
<p class="validation-summary-errors">{{forms.ValidationError}}</p>
{% endif %}
</div>
</div>
<div class="col-md-12 no-padding">
<div class="col-md-6 no-padding">
<input type="hidden" name="next" value="/" />
<input type="submit" value="Iniciar Sesión" class="btn btn-default" />
</div>
<div class="col-md-6">
<div class="checkbox">
<label style="color:#222;">
<input type="checkbox" > Recordar?
</label>
</div>
</div>
</div>
</form>
当信息错误重定向到/ login,但没有验证inhow我可以在重新加载页面之前进行表单验证吗?