使用 class 基本视图在 django3 中登录表单

Login form in django3 using class base view

我在 Django 3 的 class 基础登录中卡住了最后几个小时

当我提交用户名和密码时,出现以下错误

Using the URLconf defined in projectname.urls, Django tried these URL patterns, in this order:

admin/
[name='homepage']
signup/ [name='signup']
login/ [name='login']
^static/(?P<path>.*)$
^media/(?P<path>.*)$
dashboard/
socialcard/
^static/(?P<path>.*)$
^media/(?P<path>.*)$
The current path, login/post, didn’t match any of these.

URL 模式路径

    path("signup/", views.SignUp.as_view(), name='signup'),

   path('login/', auth_views.LoginView.as_view(), name='login'),

视图函数

class SignUp(generic.CreateView):
    form_class=UserCreationForm
    success_url=reverse_lazy('socialcard')
    template_name='registration/signup.html'

设置文件。

LOGIN_URL='login'
LOGIN_REDIRECT_URL='dashboard'
LOGOUT_REDIRECT_URL='homepage'

注册模型工作正常,但是当我提交表单时登录模型显示以下错误...

这是注册所在模板内的注册文件夹中的表单调用。

<form action="post">
{% csrf_token %}

{{form}}
<button type="submit">Login</button>
</form>

您写了 action="post",html 会将其视为 url 提交后指向表单的位置,

这就是它显示此错误的原因

The current path, login/post, didn’t match any of these.

而您需要的是将其指定为方法,如下所示:

<form method="post">
 {% csrf_token %}
 {{form}}
 <button type="submit">Login</button>
</form>

现在,它应该可以正常工作了