I have a flatpage in my website. I created it by writing this:
urlpatterns = [path('admin/', admin.site.urls),path('facts/', include('django.contrib.flatpages.urls')),]
After that in admin page I created a page about-me, so the whole url for that page is ‘localhost/facts/about-me/’
I tried to write test for this page:
class StaticURLTests(TestCase):
def setUp(self):
self.guest_client = Client()
def test_about_author(self):
response = self.guest_client.get('/facts/about-me/')
self.assertEqual(response.status_code, 200)
But I get the following error:
self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200
FAILED (failures=1)
Destroying test database for alias ‘default’…
Can’t figure out why. Maybe smb was facing the same problem.
I thought that the error was because of the flatpage but, the same error occured when I tried to test url of the Task model
urlpatterns = [path('task/<slug:slug>/',views.task, name='task'),]
Test:
@classmethod
def setUpClass(cls):
super().setUpClass()
Task = Task.objects.create(
title='Title',
slug=' test-slug',
description='Desc',
)
def setUp(self):
self.guest_client = Client()
user = get_user_model()
self.user = user.objects.create_user(username='Bob')
def test_task_url_exists_at_desired_location_authorized(self):
response = self.guest_client.get('/task/test-slug/')
self.assertEqual(response.status_code, 200)
I tried to change response = self.guest_client.get('/task/test-slug/')
to response = self.guest_client.get(reverse('/task/'))
but nothing seems to work.
question from:
https://stackoverflow.com/questions/65869148/testing-urls-django 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…