在 django-template 中添加 url

adding a url in django-template

我试图在 django 模板中添加一个 url,据说它可以注销用户

<div><a href="{% url 'accounts.views.user_logout' %}">logout</a></div> 

但它抛出了 NoReverseMatch

模板:

<header class="grid-header header">
    <div class='logo-text'>Nitro fleet</div>
    <div class="profile-detail">
        {% if user.is_authenticated %}
        <img class="profile-img" src="{{user.picture.url}}" />
        <div>
        <div>{{user.username}}</div>
        <div><a href="{% url 'accounts.views.user_logout' %}">logout</a></div>
        </div>
        {%endif%}
    </div>
</header>

具有注销视图的应用程序(帐户)的url:

from django.urls import path
from . import views
appname = 'accounts'
urlpatterns = [
    path('register', views.register_view , name='user_register'),
    path('login', views.user_login , name='user_login'),
    path('logout', views.user_logout , name='user_logout'),
]

视图函数:

def user_logout(request):
    logout(request)
    return HttpResponseRedirect(request.path_info)

完整的错误日志:

NoReverseMatch at /maintenance/
Reverse for 'accounts.views.user_logout' not found. 'accounts.views.user_logout' is not a valid view function or pattern name.
Request Method: GET
Request URL:    http://127.0.0.1:8000/maintenance/
Django Version: 3.2.9
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'accounts.views.user_logout' not found. 'accounts.views.user_logout' is not a valid view function or pattern name.
Exception Location: C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable:  C:\Users\PC\AppData\Local\Programs\Python\Python310\python.exe
Python Version: 3.10.0
Python Path:    
['C:\Users\PC\Desktop\nitrofleet',
 'C:\Users\PC\AppData\Local\Programs\Python\Python310\python310.zip',
 'C:\Users\PC\AppData\Local\Programs\Python\Python310\DLLs',
 'C:\Users\PC\AppData\Local\Programs\Python\Python310\lib',
 'C:\Users\PC\AppData\Local\Programs\Python\Python310',
 'C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages']
Server time:    Mon, 31 Jan 2022 20:43:51 +0000

只需使用视图的名称: {% url 'accounts:user_logout' %}