尝试注销时的Django NoReverseMatch

时间:2019-10-29 05:19:51

标签: django

我有一个Django视图函数signout,我想调用它的表单/模板。当我单击注销时,它将调用该函数,但重定向后不起作用。我收到消息Reverse for '<WSGIRequest: POST '/account/signout'>' not found. '<WSGIRequest: POST '/account/signout'>' is not a valid view function or pattern name.

Urls.py

    urlpatterns = [
    path('register', views.register, name='register'),
    path('login', views.login, name='login'),
    path('signout', views.signout, name='signout'),
    path('dashboard', views.dashboard, name='dashboard'),
]

   Views.py

    def signout(request):

        if(request.method == 'POST'):
            auth.logout(request)
            messages.success(request, 'You are logged out!')
            return redirect(request, 'index')
   Template form
     <form action="{% url 'signout'%}" id="logout" method="POST">
              {% csrf_token %}
              <input type="hidden">
              <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-sign-out-alt"></i>
              Log Out</button>
     </form>

出了什么问题??其余路由工作正常,并声明它们与退出相同。

1 个答案:

答案 0 :(得分:0)

您的观点做错了。您在检查if条件和重定向不带request参数时不需要括号。

这样更改您的视图

     def signout(request):
        if request.method == 'POST':
            auth.logout(request)
            messages.success(request, 'You are logged out!')
            return redirect('index')