Andrew
Andrew

Reputation: 1

Еhere was an error while adding url to test

When I transfer to the test url, an error pops up:

Reverse for 'movie' not found. 'movie' is not a valid view function or pattern name.

Here is my self test:

class BooksApiTestCase(APITestCase):
def setUp(self):
    self.movie_1 = Movie.objects.create(title="terminator", year="1990", rating="5",url="retminator")
    self.movie_2 = Movie.objects.create(title="robocop", year="1991", rating="4",url="robocop")
    self.movie_3 = Movie.objects.create(title="rembo", year="1992", rating="3",url='rembo')

def test_get(self):
    url = reverse('movie')
    print(url)
    response = self.client.get(url)
    serializer_data = MovieListSerializer([self.movie_1, self.movie_2, self.movie_3], many=True).data
    self.assertEqual(status.HTTP_200_OK, response.status_code)
    self.assertEqual(serializer_data, response.data)

Here is my self url:

    urlpatterns = format_suffix_patterns([
path("movie/", views.MovieViewSet.as_view({'get': 'list'})), 

Upvotes: 0

Views: 35

Answers (1)

Massimo Costa
Massimo Costa

Reputation: 1860

Hi @Andrew and Welcome to StackOverflow.

To use reverse() you have to properly set the view's name in the urlpatterns

urlpatterns = format_suffix_patterns([
    path("movie/", views.MovieViewSet.as_view({'get': 'list'}, name='movie')
]),

Refer to the official documentation

Upvotes: 1

Related Questions