登录表单在django项目的下拉引导中 [英] login form in dropdown bootstrap on django project

查看:90
本文介绍了登录表单在django项目的下拉引导中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在django项目的下拉列表(bootstrap)中创建并验证一个登录表单(用户和密码),但表单永远不会被创建,从未被验证,我真的不知道该怎么办是,这是代码(简而言之,我不能在自举下拉列表中放置一个表单):

I've been trying to create and validate a login form (user and password) in a dropdown list (bootstrap) in a django project, but the form is never created and never validated, i really have no idea what could it be, this is the code (in resume, I can't put a form in the bootstrap dropdown):

forms.py

class LoginForm(forms.Form):

   username = forms.CharField(min_length = 2, max_length=36,label=_('Username'))

   password = forms.CharField(min_length = 4, max_length=16, widget=forms.PasswordInput(render_value=False), label=_('Password')) 

views.py

def index_view(request):
activo = "index"
secciones = seccion.objects.filter(titulo=activo)
mensaje = ""
if request.user.is_authenticated():
    return HttpResponseRedirect('/')
else:
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            usuario = authenticate(username=username,password=password)
            if usuario is not None and usuario.is_active:
                login(request,usuario)
                return HttpResponseRedirect('/')
            else:
                mensaje = "usuario y/o password incorrecto"


ctx = {'activo':activo,'seccion':secciones}
return render_to_response('home/index.html',ctx,context_instance=RequestContext(request))

code> base.html :

in base.html:

        <div class="nav-collapse"><!-- Other nav bar content -->
        <!-- The drop down mrender_to_response('home/index.html',ctx,context_instance=RequestContext(request))enu -->
            <ul class="nav pull-right sign-in">
            {% if user.is_authenticated %}
                <a id="profile-box" class="btn dropdown-info" data-toggle="dropdown" href="#">
                    <i class="icon-user"></i> {{ user.username }}
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="#">Perfil</a></li>
                    <li class="divider"></li>
                    <li><a id="sign-out-link" href="{% url vista_logout %}">Cerrar Sesion</a></li>
                </ul>
            {% else %}
                <li><a href="/accounts/register"><button class="btn btn-success"  type="submit">Registrase</button></a></li>

                <li class="divider-vertical"></li>
                <li id="sign-in-box" class="dropdown">
                <a id="sign-in-link" class="dropdown-toggle" href="#" data-toggle="dropdown"><button class="btn btn-success">Ingresar</button></a>


                <div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
                    <form class="js-signin signin" action="." method="POST" accept-charset="UTF-8">{% csrf_token %}
                            <!--{% if form %}-->                                
                                {{ form.as_p }}
                            <!--{% else %}                          
                                <p>
                                    <label for="username">usuario:</label> 
                                    <input id="username" type="text" name="username" />
                                </p>
                                <p>
                                    <label for="password">contraseña:</label> 
                                    <input id="password" type="password" name="password" />
                                </p>
                            {% endif %}-->


                                <input id="remember_me" type="checkbox" name="remember_me" value="1" />
                                <label class="string optional" for="user-remember-me"> Recordar mis datos</label>       
                    </form>

                            <button id="login-button" class="btn btn-block btn-primary" type="submit">Ingresar</button>



                </div>


                </li>           
              {% endif %}
            </ul>
        </div>


推荐答案

您忘了将表单传递给您的上下文数据。

You forgot to pass the form to your context data.

尝试这样:

ctx = {'activo':activo,'seccion':secciones,'form': form}

这篇关于登录表单在django项目的下拉引导中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆