[英]Django Reverse Not Found When Unit-Testing
I am writing unit-tests for a Django app.我正在为 Django 应用程序编写单元测试。 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.
django.urls.exceptions.NoReverseMatch:找不到“video_uploader.list_videos”的反向。 'video_uploader.list_videos' is not a valid view function or pattern name.
“video_uploader.list_videos”不是有效的视图 function 或模式名称。
app/tests.py应用程序/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应用程序/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 .
应用程序名称和 url 名称之间应该是
:
而不是.
. . Try with:
尝试:
url = reverse("video_uploader:list_videos")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.