gpt4 book ai didi

python - 错误 : 'login' not found. 'login' 不是有效的 View 函数或模式名称

转载 作者:行者123 更新时间:2023-12-04 01:53:11 31 4
gpt4 key购买 nike

这是我第一次使用 Django 登录模板,但遇到了问题。它一直说 login 不是一个有效的 View 函数,但我在我的用户应用程序的 urls.py 中定义了它:

enter image description here

enter image description here

不知道我做错了什么。

主 urls.py

urlpatterns = [
path('', include('blogging_logs.urls', namespace='blogging_logs')),
path('users/', include('users.urls', namespace='users')),
path('admin/', admin.site.urls),
]

应用程序: blogging_logs:base.html
<p>
<a href="{% url 'blogging_logs:index' %}">Home</a>
<a href="{% url 'blogging_logs:categories' %}">Categories</a>
{% if user.is_authenticated %}
Hello, {{ user.username }}.
{% else %}
<a href="{% url 'users:login' %}"> login in</a>
{% endif %}
</p>

{% block content %}{% endblock content %}

应用程序:用户:urls.py
from django.contrib import admin
from django.urls import re_path, path
from django.contrib.auth import authenticate, login
from django.contrib.auth import views as auth_views

app_name = 'users'

urlpatterns = [
# Login Page
path('login/', auth_views.LoginView.as_view(template_name='login.html')),
]

应用程序:用户:login.html
{% extends "blogging_logs/base.html" %}

{% block content %}
{% if form.errors %}
<p>Your username and password din't match. Please try again.</p>
{% endif %}

<form class="" method="post" action="{% url 'users:login' %}" >
{% csrf_token %}
{{ form.as_p }}
<button name='submit'> Login in </button>
<input type="hidden" name="next" value="{% url 'blogging_logs:index' %}" />
</form>
{% endblock content %}

最佳答案

您错过了 name path 的论据功能:

更改以下行

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


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

关于python - 错误 : 'login' not found. 'login' 不是有效的 View 函数或模式名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030913/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com