我正在开发 Django 项目,我正在其中实现内置身份验证框架。我直接为内置 View 创建了 url。
我应该访问以下网址 http://127.0.0.1:8000/account/login/ 它将我重定向到以下 url 并应该选择“LoginView”及其相应的模板
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('', views.dashboard, name='dashboard'),
]
这是模板代码
{% block content %}
<h1>Log-in</h1>
{% if form.errors %}
<p>
Your username and password didn't match. please try again.
</p>
{% else %}
<p>Please, use the following form to log-in: </p>
{% endif %}
<div class="login-form">
<form action="{% url 'login' %}" method="post">
{{form.as_p}}
{% csrf_token %}
<input type="hidden" name="next" value="{{next}}">
<p><input type="submit" value="Log-in"></p>
</form>
</div>
{% endblock %}
提交凭据时出现以下错误
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/profile/
Using the URLconf defined in xxxxxxxxxx.urls, Django tried these URL patterns, in this order:
admin/
account/
The current path, accounts/profile/, didn't match any of these.
请帮我解决这个问题。
请您参考如下方法:
成功登录后,Django 必须将用户重定向到特定页面。此设置可在项目的 settings.py 中访问。
根据 django 文档,默认重定向 URL 设置为/accounts/profile/。因此,您一定会收到错误消息,因为应用程序正在遵循默认值并尝试在成功登录后将用户重定向到默认 URL。
如果还没有,请尝试将以下行添加到您的 settings.py。
LOGIN_REDIRECT_URL = '/'
考虑到您希望在成功登录后将用户重定向到您的仪表板 View 。如果不是,请使用所需的值更改 URL,它应该可以工作。
