django“ noreversematch:反向”找不到”进行测试
我实施了一些测试来检查某些页面的状态代码,但是这个带有反向函数的一个测试给我带来了错误: django.urls.exceptions.noreversematch:views.views.views.adlistview'找不到。 “ ads.views.adlistview”不是有效的视图函数或模式名称。
阅读文档和堆栈溢出上的某些答案,我应该使用视图函数名称或括号内的模式名称反向功能,但它们似乎都没有用。
这是我的代码:
ads/tests/test_urls.py
from django.test import TestCase
from django.urls import reverse
class SimpleTests(TestCase):
def test_detail_view_url_by_name(self):
resp = self.client.get(reverse('ad_detail'))
# I've also tried: resp = self.client.get(reverse('ads/ad_detail'))
self.assertEqual(resp.status_code, 200)
...
ads \ urls.py
from django.urls import path, reverse_lazy
from . import views
app_name='ads'
urlpatterns = [
path('', views.AdListView.as_view(), name='all'),
path('ad/<int:pk>', views.AdDetailView.as_view(), name='ad_detail'),
...
]
mySite/urls.py
from django.urls import path, include
urlpatterns = [
path('', include('home.urls')), # Change to ads.urls
path('ads/', include('ads.urls')),
...
]
ads/views.py
class AdDetailView(OwnerDetailView):
model = Ad
template_name = 'ads/ad_detail.html'
def get(self, request, pk) :
retrieved_ad = Ad.objects.get(id=pk)
comments = Comment.objects.filter(ad=retrieved_ad).order_by('-updated_at')
comment_form = CommentForm()
context = { 'ad' : retrieved_ad, 'comments': comments, 'comment_form': comment_form }
return render(request, self.template_name, context)
。
I implemented some tests to check the status code of some pages, but this one with the reverse function throws me the error: django.urls.exceptions.NoReverseMatch: Reverse for 'ads.views.AdListView' not found. 'ads.views.AdListView' is not a valid view function or pattern name.
Reading the documentation and some answers on Stack Overflow I'm supposed to use either the view function name or the pattern name inside the parenthesis of the reverse function, but none of them seems to work.
Here's my code:
ads/tests/test_urls.py
from django.test import TestCase
from django.urls import reverse
class SimpleTests(TestCase):
def test_detail_view_url_by_name(self):
resp = self.client.get(reverse('ad_detail'))
# I've also tried: resp = self.client.get(reverse('ads/ad_detail'))
self.assertEqual(resp.status_code, 200)
...
ads\urls.py
from django.urls import path, reverse_lazy
from . import views
app_name='ads'
urlpatterns = [
path('', views.AdListView.as_view(), name='all'),
path('ad/<int:pk>', views.AdDetailView.as_view(), name='ad_detail'),
...
]
mysite/urls.py
from django.urls import path, include
urlpatterns = [
path('', include('home.urls')), # Change to ads.urls
path('ads/', include('ads.urls')),
...
]
ads/views.py
class AdDetailView(OwnerDetailView):
model = Ad
template_name = 'ads/ad_detail.html'
def get(self, request, pk) :
retrieved_ad = Ad.objects.get(id=pk)
comments = Comment.objects.filter(ad=retrieved_ad).order_by('-updated_at')
comment_form = CommentForm()
context = { 'ad' : retrieved_ad, 'comments': comments, 'comment_form': comment_form }
return render(request, self.template_name, context)
Any idea of what is causing the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在
urls.py
中使用app_name =…
,因此您需要以视图的名义将其指定为名称空间,因此ads : ad_detail
,并指定一个主键:因此,在这里,我们访问
42
的URL用作pk
url参数的值。Since you use an
app_name=…
in yoururls.py
, you need to specify this as a namespace in the name of the view, soads:ad_detail
, and specify a primary key:So here we visit the URL where
42
is used as value for thepk
URL parameter.