如何解决NoReverseMatch

问题描述 投票:0回答:1

我有项目的urls.py文件,它位于其中的forecast/urls,我正在尝试为我的应用程序的URL设置URL路由

from django.contrib import admin                             
from django.urls import path,include                       
from django.contrib.auth import login                         
       urlpatterns = [
            path('auth/',include
                     ('authorization.urls')),
            ]

authorization/urls.py

from django.urls import path                               
from . import views                                        
        urlpatterns = [
             path('login/',
             views.login_name), 
         ]

运行服务器后,我想打开http://127.0.0.1:8000/auth/login/,我得到了

NoReverseMatch at/   
auth/login/
 Reverse for 'login' not found. 'login' is 
 not a valid view function or pattern    
 name.

根据我的url设置,我希望项目urls.py的路径功能将提供给包含的授权/urls.py路径auth/,而不是authorization/urls.py的第二条路径将比构造的login添加到模式auth/login/如果我是对的,将给出我的视图函数,为什么会出现NoReverse错误。谁能指导我我做错了

我的完整追溯在这里

Reverse for 'login' not found. 'login' is not a valid view function or pattern name.Request Method:GETRequest URL:http://127.0.0.1:8000/auth/login/Django Version:2.2.4Exception Type:NoReverseMatchException Value:Reverse for 'login' not found. 'login' is not a valid view function or pattern name.Exception Location:/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 668Python Executable:/data/data/com.termux/files/home/storage/predictions/env/bin/pythonPython Version:3.7.4Python Path:['/data/data/com.termux/files/home/storage/predictions/forecast', '/data/data/com.termux/files/home/storage/predictions/forecast', '/data/data/com.termux/files/home/storage/predictions/env/lib/python37.zip', '/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7', '/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/lib-dynload', '/data/data/com.termux/files/usr/lib/python3.7', '/data/data/com.termux/files/home/storage/predictions/env/lib/python3.7/site-packages']Server time:Mon, 30 Dec 2019 06:58:18 +0000
django python-3.x django-urls
1个回答
0
投票
您需要给URL起一个名字,以便在您想要反转它时可以使用它

path('login/', views.login_name, name='login'),

© www.soinside.com 2019 - 2024. All rights reserved.