在我的项目的基本模板中,如果您尚未经过身份验证,我在标题中有一段关于登录的代码。这是通过一个正常工作的下拉菜单制作的。
问题发生在尝试使其正常运行时,我同时拥有表单和视图,但在尝试将字段放入模板时,它们没有显示出来。也许这是一个导入问题,我不知道如何解决它。这是代码:
模板(base.html):
<div id="auth">
{% if user.is_authenticated %}
<p>Bienvenido, <b>{{ user.username }} | <a href=" {% url "logout" %}">Logout</a></b></p>
{% else %}
<div id="navthing">
<h2><a href="#" id="loginform">Log in</a> | <a href="#">Sign Up</a></h2>
<div class="login">
<div class="arrow-up"></div>
<div class="formholder">
<div class="randompad">
<form action="" method="POST">{% csrf_token %}
{% if message %}
<b>{{message}}</b>
{% endif %}
{{form.as_p}}
<input type="submit" value="Login" />
</form>
</div>
</div>
</div>
</div>
{% endif %}
</div>
views.py:
def login_page(request):
message = None
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
message = "Now you are succesfully logged in"
else:
message = "Your username is inactive"
else:
message = "Your username and/or password is incorrect"
else:
form = LoginForm()
return render_to_response('base.html', {'message': message, 'form': form},
context_instance=RequestContext(request))
forms.py:
from django import forms
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput())
这里的问题在哪里?谢谢。