Python 断言错误:404!=在Django发生的200次故障

Python 断言错误:404!=在Django发生的200次故障,python,django,Python,Django,我为我的模型的所有方面编写了测试,但非常简单和基本的测试失败了。下面是url.py app_name = 'projects' urlpatterns = [ path('all/', ViewProjects.as_view(), name='all_projects'), path('project/<slug:project_slug>/', ProjectDetail.as_view(), name='project_detail'),

我为我的模型的所有方面编写了测试,但非常简单和基本的测试失败了。下面是
url.py

app_name = 'projects'
urlpatterns = [
    path('all/', ViewProjects.as_view(), name='all_projects'),
    path('project/<slug:project_slug>/',
         ProjectDetail.as_view(), name='project_detail'),
    path('new/', ProjectCreateView.as_view(), name='project_create'),
    path('', HomeView.as_view(), name='home'),
]
此测试失败,以下是回溯:

(Portfolio) PS D:\GitHub\Portfolio> python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.[2020-09-20 21:34:36,616] log: WARNING - Not Found: /
F
======================================================================
FAIL: test_project_list_view (projects.tests.TestProject)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\GitHub\Portfolio\projects\tests.py", line 11, in test_project_list_view
    self.assertEquals(response.status_code, 200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 2 tests in 0.113s

FAILED (failures=1)
Destroying test database for alias 'default'...
但是,当我运行
python manage.py runserver
时,URL、视图和模板都是连接的。有什么问题?请帮忙,谢谢

编辑:


运行测试时,测试数据库中似乎没有任何装置。 方法“get_context_data”用于“get_object_或_404”。似乎提到的某个类没有任何实例,因此您运行此断言错误。
有时,当反向URL设置错误时,您可能会遇到此错误。

您的
HomeView的
获取
的功能是什么?它是否使用了类似于
get\u object\u或\u 404
的方法,从而导致404?(请注意,您的测试使用自己的数据库,而不是您的应用程序运行时使用的数据库。)我刚刚添加了视图,我只检查了请求状态代码,而且我还创建了自己的数据库,没有一个起作用。我尝试了我知道的所有可能的方法。你的代码似乎没有问题。您的测试失败,因为在您的视图中,您调用了
get\u object\u或\u 404
,以查找数据库中不存在的一些数据,因为django测试使用了一个测试数据库,该数据库在每次测试后都是清除的。因此,为了使代码正常运行,您必须手动填充这些数据,例如在
类别中
模型中。非常感谢,就是这样
(Portfolio) PS D:\GitHub\Portfolio> python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.[2020-09-20 21:34:36,616] log: WARNING - Not Found: /
F
======================================================================
FAIL: test_project_list_view (projects.tests.TestProject)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\GitHub\Portfolio\projects\tests.py", line 11, in test_project_list_view
    self.assertEquals(response.status_code, 200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 2 tests in 0.113s

FAILED (failures=1)
Destroying test database for alias 'default'...
class HomeView(ListView):
    model = Project
    template_name = 'index.html'
    context_object_name = 'projects'

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['projects'] = Project.objects.all()
        react = get_object_or_404(Category, title='reactjs')
        context['react_projects'] = Project.objects.filter(
            category__title=react.title)
        django = get_object_or_404(Category, title='django')
        context['django_projects'] = Project.objects.filter(
            category__title=django.title)
        context['featured_blogs'] = Blog.objects.filter(featured=True)
        return context