Django:登录用户并在同一页面刷新而不定义模板? [英] Django: login user and refresh on same page without defining a template?
问题描述
我正在尝试使用自举下拉式登录表单让用户登录。我可以硬编码我的用户名和密码,并验证正常,但我试图有一个用户登录,而不去登录屏幕。这是我的代码:
模板:
我使用动作调用logUserIn url,以便表单可以发布到该视图。
< ul class =nav pull-right>
{%if user.is_authenticated%}
< li>< a>欢迎{{user.first_name}}< / a>< / li>
< li class =divider-vertical>< / li>
< li>< a href ={%url'caesarWorkflowApp.views.logUserOut'%}>注销< / a>< / li>
< li class =divider-vertical>< / li>
{%else%}
< li class =dropdown>
< a class =dropdown-togglehref =#data-toggle =dropdown>登录< strong class =caret>< / strong>< / a>
< div class =dropdown-menustyle =padding:15px; padding-bottom:0px;>
< form action ={%url'caesarWorkflowApp.views.logUserIn'%}method =postaccept-charset =UTF-8>
{%csrf_token%}
< input id =user_usernamestyle =margin-bottom:15px; type =textname =usernameplaceholder =Usernamesize =30/>
< input id =user_passwordstyle =margin-bottom:15px; type =passwordname =passwordplaceholder =Passwordsize =30/>
< input id =user_remember_mestyle =float:left; margin-right:10px; type =checkboxname =user [remember_me]value =1/>
< label class =string optionalfor =user_remember_me>记住我< / label>
< input class =btn btn-primarystyle =clear:left; width:100%; height:32px; font-size:13px; type =submitname =commitvalue =登录/>
< / form>
< / div>
< / li>
{%endif%}
< li class =divider-vertical>< / li>
< / ul>
URLS:
urlpatterns = patterns('',
url(r'^ $','caesarWorkflowApp.views.home',name ='default'),
url(r'^ $','caesarWorkflowApp.views .logUserIn'),
url(r'^ $','caesarWorkflowApp.views.logUserOut'),
)
VIEWS:
然后我有这个登录视图不起作用,因为即使表单成功发布,控制台甚至不打印输入。我发错了吗称错误行为? 有没有人知道如何创建登录/注销视图,而不创建一个模板,以便它只是刷新/重定向到主页?
def logUserIn(request):
if request.method =='POST':
print'enter'
username = request.POST ['username ']
password = request.POST ['password']
user = authenticate(username = username,password = password)
如果用户不是没有:
#验证密码对于用户
如果user.is_active:
登录(请求,用户)
打印(用户有效,活动和验证)
else:
print(密码有效,但帐户已被禁用!)
其他:
#验证系统无法验证用户名和密码
print(用户名密码不正确。)
response = render_to_response('home.html',{},context_instance = RequestContext(request))
返回响应
我也希望页面不要问我是否要重新加载,因为它的帖子形式,我相信这是解决方案,以防任何人对此感兴趣:停止要刷新刷新表单数据的浏览器?
谢谢!
我无法让AJAX工作,所以我想出了这个解决方案: p>
TEMPLATE:
< ul class =nav pull-right >
{%if user.is_authenticated%}
< li>< a>欢迎,{{user.first_name}}< / a>< / li>
< li class =divider-vertical>< / li>
< li>< a href =/ logout />注销< / a>< / li>
< li class =divider-vertical>< / li>
{%else%}
< li class =dropdown>
< a class =dropdown-togglehref =#data-toggle =dropdown>登录< strong class =caret>< / strong>< / a>
< div class =dropdown-menustyle =padding:15px; padding-bottom:0px;>
< form id =form-loginaction =/method =postaccept-charset =UTF-8>
{%csrf_token%}
< input id =user_usernamestyle =margin-bottom:15px; type =textname =usernameplaceholder =Usernamesize =30/>
< input id =user_passwordstyle =margin-bottom:15px; type =passwordname =passwordplaceholder =Passwordsize =30/>
< input id =user_remember_mestyle =float:left; margin-right:10px; type =checkboxname =user [remember_me]value =1/>
< label class =string optionalfor =user_remember_me>记住我< / label>
< input class =btn btn-primarystyle =clear:left; width:100%; height:32px; font-size:13px; type =submitname =commitvalue =登录/>
< / form>
< / div>
< / li>
< li class =divider-vertical>< / li>
{%endif%}
< / ul>
URLS:
code> urlpatterns = patterns('',
url(r'^ $','caesarWorkflowApp.views.home',name ='default'),
url(r'^ login / $','caesarWorkflowApp.views.logUserIn'),
url(r'^ logout / $','caesarWorkflowApp.views.logUserOut'),
)
查看:
def logUserIn(request) :
print'enter'
username = request.POST ['username']
password = request.POST ['password']
user = authenticate(username = username,password = password)
如果用户不是没有:
#验证用户的密码
如果user.is_active:
登录(请求,用户)
print(用户有效,有效和认证)
其他:
打印(密码有效,但帐户已被禁用!)
其他:
#认证系统是无法验证用户名和密码
print(用户名和密码不正确)
response = render_to_response('home.html',{},context_instance = RequestContext(request) )
return response
def logUserOut(request):
logout(request)
return HttpResponseRedirect('/')
完美工作,真好看! =)
I'm trying to use a bootstrap drop-down sign in form to have users login. I'm able to hard code my username and password and authenticate just fine but I'm trying to have a user login without going to a login screen. Here is my code:
Template: I use action to call the logUserIn url so that the form can post to that view.
<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>
URLS:
urlpatterns = patterns('',
url(r'^$', 'caesarWorkflowApp.views.home', name='default'),
url(r'^$', 'caesarWorkflowApp.views.logUserIn'),
url(r'^$', 'caesarWorkflowApp.views.logUserOut'),
)
VIEWS: Then I have this view for login which doesn't work because console never even print 'enter' even though the form is posting successfully. Am I posting wrong? Calling the wrong action? Does anyone know how to create a login/logout view without creating a template for it so that it just refreshes/redirects to the home page?
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
I also want the page to not ask me if I want to reload since the it's post form and I believe this is the solution in case anyone is interested in that: Stop browsers asking to resend form data on refresh?
Thank you!
I couldn't get AJAX working so I just came up with this solution:
TEMPLATE:
<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="/logout/">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 id="form-login" action="/" 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>
<li class="divider-vertical"></li>
{% endif %}
</ul>
URLS:
urlpatterns = patterns('',
url(r'^$', 'caesarWorkflowApp.views.home', name='default'),
url(r'^login/$', 'caesarWorkflowApp.views.logUserIn'),
url(r'^logout/$', 'caesarWorkflowApp.views.logUserOut'),
)
VIEWS:
def logUserIn(request):
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
def logUserOut(request):
logout(request)
return HttpResponseRedirect('/')
Works perfectly and really nice looking! =)
这篇关于Django:登录用户并在同一页面刷新而不定义模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!