2
我一直試圖在django項目的下拉列表(引導程序)中創建和驗證登錄表單(用戶和密碼),但該表單從未被創建並且從未被驗證過,我真的不知道會不會是,這是代碼(在簡歷,我不能把這樣的形式,引導下拉菜單):在django項目的下拉引導上登錄表單
在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))
在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>
非常感謝你,這是原因之一,並且按鈕是出於形式。 – user1522096 2013-03-03 20:23:20