我正在尝试使用引导下拉登录表单让用户登录。我可以硬编码我的用户名和密码并进行身份验证,但我试图让用户登录而不进入登录屏幕。这是我的代码:
模板:我使用操作调用 logUserIn url,以便表单可以发布到该视图。
<ul class="nav pull-right">
{% if user.is_authenticated %}
<li><a> Welcome {{ user.first_name }} </a></li>
<li class="divider-vertical"></li>
<li><a href="{% url 'caesarWorkflowApp.views.logUserOut' %}">Log Out</a></li>
<li class="divider-vertical"></li>
{% else %}
<li class="dropdown">
<a class="dropdown-toggle" href="#" data-toggle="dropdown">Sign In <strong class="caret"></strong></a>
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
<form action="{% url 'caesarWorkflowApp.views.logUserIn' %}" method="post" accept-charset="UTF-8">
{% csrf_token %}
<input id="user_username" style="margin-bottom: 15px;" type="text" name="username" placeholder="Username" size="30" />
<input id="user_password" style="margin-bottom: 15px;" type="password" name="password" placeholder="Password" size="30" />
<input id="user_remember_me" style="float: left; margin-right: 10px;" type="checkbox" name="user[remember_me]" value="1" />
<label class="string optional" for="user_remember_me"> Remember me</label>
<input class="btn btn-primary" style="clear: left; width: 100%; height: 32px; font-size: 13px;" type="submit" name="commit" value="Sign In" />
</form>
</div>
</li>
{% endif %}
<li class="divider-vertical"></li>
</ul>
网址:
urlpatterns = patterns('',
url(r'^$', 'caesarWorkflowApp.views.home', name='default'),
url(r'^$', 'caesarWorkflowApp.views.logUserIn'),
url(r'^$', 'caesarWorkflowApp.views.logUserOut'),
)
视图:然后我有这个登录视图,它不起作用,因为即使表单发布成功,控制台也不会打印“输入”。我发错了吗?调用错误的操作?有谁知道如何在不为其创建模板的情况下创建登录/注销视图,以便它只刷新/重定向到主页?
def logUserIn(request):
if request.method == 'POST':
print 'enter'
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
# the password verified for the user
if user.is_active:
login(request, user)
print("User is valid, active and authenticated")
else:
print("The password is valid, but the account has been disabled!")
else:
# the authentication system was unable to verify the username and password
print("The username and password were incorrect.")
response = render_to_response('home.html', {}, context_instance=RequestContext(request))
return response
我还希望页面不要问我是否要重新加载,因为它是发布表单,我相信这是如果有人对此感兴趣的解决方案:停止浏览器要求在刷新时重新发送表单数据?
谢谢!