我对django的登录感到困惑。所以我希望用户通过我的下拉菜单登录,但我的LoginForm没有显示。我不知道该怎么做
forms.py
class LoginForm(forms.Form):
username = forms.CharField(label=(u'Username'),widget = forms.TextInput(attrs = {'placeholder': 'Username'}))
password = forms.CharField(max_length=16, label=(u'Password'),
widget = forms.PasswordInput(attrs = {'placeholder': 'Password (min 8 chrct)'}, render_value = False),
validators=[MinLengthValidator(8, message = 'Password must be at least 8 characters')])
views.py
def LoginRequest(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
tutor = authenticate(username=username, password=password)
if tutor is not None:
login(request, tutor)
return HttpResponseRedirect('/profile/')
else:
return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
else:
return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
else:
form = LoginForm()
context = {'form': form}
return render_to_response('/', {'form': form}, context_instance=RequestContext(request))
navbar.html
<a class="dropdown-toggle" href="#" data-toggle="dropdown">Login <strong class="caret"></strong></a>
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
<form action="" method="POST">
{% csrf_token %}
{% if form.errors %}
{{ form.errors}}
{% endif %}
{{form}}
<input class="btn btn-primary" type="submit" value="Login" />
</form>
</div>
答案 0 :(得分:1)
It looks like your issue is not with Django but with your bootstrap drop down.
将您的下拉html包裹在<div class="dropdown">
。
<div class="dropdown">
<a class="dropdown-toggle" href="#" data-toggle="dropdown">Login <strong class="caret"></strong></a>
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
<form action="" method="POST">
{% csrf_token %}
{% if form.errors %}
{{ form.errors }}
{% endif %}
{{ form }}
<input class="btn btn-primary" type="submit" value="Login"/>
</form>
</div>
</div>
答案 1 :(得分:-1)
<a class="dropdown-toggle" href="#" data-toggle="dropdown">Login <strong class="caret"></strong></a>
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
<form action="" method="POST">
{% csrf_token %}
{% if login_form.errors %}
{{ login_form.errors}}
{% endif %}
{{ login_form }}
<input class="btn btn-primary" type="submit" value="Login" />
</form>
</div>