[英]django test client returns 404, but works in the shell
This code works fine in the shell, but if you run it through python manage.py test
it throws a 404 error, what could be the problem?此代码在 shell 中运行良好,但如果通过
python manage.py test
运行它会引发 404 错误,可能是什么问题?
test_urls.py test_urls.py
from django.test import Client, TestCase
class StaticURLTests(TestCase):
def setUp(self):
self.guest_client = Client()
def test_homepage(self):
response = self.guest_client.get("/")
self.assertEqual(response.status_code, 200)
error:错误:
❯ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_homepage (posts.tests.test_urls.StaticURLTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/finegorko/Development/Yandex.Practicum/hw03_forms/yatube/posts/tests/test_urls.py", line 10, in test_homepage
self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200
----------------------------------------------------------------------
Ran 1 test in 0.003s
FAILED (failures=1)
Destroying test database for alias 'default'...
The problem was that a test database was being created that did not have a group.问题是正在创建一个没有组的测试数据库。 In
views.py
an object with the function get_object_or_404()
was passed to the index context.在
views.py
中,一个 object 和 function get_object_or_404()
被传递给索引上下文。
def index(request):
...
group = get_object_or_404(Group) # <--
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.