login with ajax,jquery - login form with ajax and django - Stack Overflow

My app has a the login option, i am trying when the user sumbit the login form, this validate and if is not right say something like user and password doesnt exist, but without my main page reload or redirect to another page, the login form is in the same index page

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)

i am using the Ajax from the views

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

{% csrf_token %}

{{ formlog.username }}

{%if form.errors %}

{{forms.ValidationError}}

{% endif %}

{{ formlog.password }}

{%if form.errors %}

{{forms.ValidationError}}

{% endif %}

Recordar?

when the informations is wrong redirec to /login, but no validate the inhow can i do the form validate before it reload the page?.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值