简体   繁体   中英

Django Reverse Not Found When Unit-Testing

I am writing unit-tests for a Django app. The app works as expected. However, one of the new tests fails because the system is not able to find a reverse match for a view name. What am I missing?

django.urls.exceptions.NoReverseMatch: Reverse for 'video_uploader.list_videos' not found. 'video_uploader.list_videos' is not a valid view function or pattern name.

app/tests.py

from django.test import TestCase
from .models import Video
from .views import *
from django.db import models
from django.utils import timezone
from django.urls import reverse

class VideoTest(TestCase):

    def create_video(self, name="Test Video", creation_date=timezone.now, videofile="/video/"):
        return Video.objects.create(name=name, videofile=videofile)

    def test_video_creation(self):
        video = self.create_video()
        self.assertTrue(isinstance(video, Video))
        self.assertEqual(video.__str__(), video.name + ": " + str(video.videofile))

    def test_videos_list_view(self):
        video = self.create_video()
        url = reverse("video_uploader.list_videos")
        response = self.client.get(url)

        self.assertEqual(response.status_code, 200)
        self.assertIn(video.name, response.content)

app/urls.py

from django.urls import path

from . import views

app_name = 'video_uploader'
urlpatterns = [
    path('upload', views.upload_video, name='upload_video'),
    path('', views.list_videos, name='list_videos'),
]

Between the app name and the url name should be a : not a . . Try with:

url = reverse("video_uploader:list_videos")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM