NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch:未找到 'home' 的反转。 'home' 不是有效的视图函数或模式名称

NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'home' not found. 'home' is not a valid view function or pattern name

我刚开始使用 Django,我正在尝试为我的页面应用程序编写测试,但在测试主页 url 名称

时,我总是遇到同样的错误

urls.py

from django.urls import path
from pages.views import HomePageView

app_name = 'pages'

urlpatterns = [
     path('', HomePageView.as_view(), name='home'),
]

test.py

from django.test import SimpleTestCase
from django.urls import reverse

class HomePageTest(SimpleTestCase):

    def test_homepage_url(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)

    def test_homepage_name(self):
        url = reverse('home')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_homepage_template(self):
        response = self.client.get('/')
        self.assertTemplateUsed(response, 'home.html')

views.py

from django.shortcuts import render
from django.views.generic import TemplateView

class HomePageView(TemplateView):
    template_name = 'pages/home.html'

pages/home.html

{% extends 'base.html' %}


{% block title %}Home{% endblock title %}


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

您没有指定,但我认为是 test_homepage_name 给出了错误?

尝试包含应用名称:

url = reverse('pages:home')