使用ajax和djang的登录表单

2024-04-18 12:18:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我的应用程序有一个登录选项,我正在尝试当用户输入登录表单时,这个验证和如果不对的话,比如说用户和密码不存在,但是没有我的主页重新加载或重定向到另一个页面,登录表单在同一个索引页中

视图.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

表单.py

^{pr2}$

登录.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>

当信息不正确时,请重新登录,但没有验证。在重新加载页面之前,我可以在表单上执行验证吗?。在


Tags: divformdatagetifemailrequestusername