Py学习  »  问与答

使用auth.views.login无法实现登陆的问题,求大神指点。

lunch650 • 9 年前 • 3373 次点击  

初学django,现在感觉很迷茫,今天遇到了一个难缠的问题,弄了一晚上没弄出来,想请社区的大神们帮忙看看问题出在哪里。

#urls.py
urlpatterns = [
    url(r'^$',decorators.login_required(IndexView.as_view(),login_url='HRSystem:login'),
        name='index'
        ),
    url(r'^login/$',auth_views.login,{'template_name' : 'HRSystem/login.html',},
        name='login'
        ),
    url(r'^logout/$',auth_views.logout_then_login,{'login_url' : 'HRSystem:index'},
        name='logout'
        ),

想法是进入index的时候验证是否登陆,如果没有登陆则进入到login页面。

#views.py
class IndexView(generic.TemplateView):
    template_name = 'HRSystem/index.html'

简单的视图,首页就是一个展示页面

#login.html
<form method="post" action="{% url 'HRSystem:index'%}">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="登陆">
</form>

主要问题是出在这里,想法是登陆成功后直接跳转到index页面,如果登陆失败则停留在本页面中,这些都是今天看auth.views.login()函数时候学到的。但是无论我正确的或者是错误的用户名密码,都无法实现跳转也没有报错信息0,而是继续停留在login页面中。 后来无意中将login页面form的action删除以后,一切都正常了。 我看了官方文档上是这样:

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
    <td>{{ form.username.label_tag }}</td>
    <td>{{ form.username }}</td>
</tr>
<tr>
    <td>{{ form.password.label_tag }}</td>
    <td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>

但是我把这里面的action放到我自己的页面中就报错了。

这种情况不知道各位大神有没有遇到过,我现在疑惑的是问题出在哪里,要是我登陆成功后不想跳转到index页面而是其他页面,应该怎么处理?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1390
 
3373 次点击  
文章 [ 1 ]  |  最新文章 9 年前