django“ noreversematch:反向”找不到”进行测试

发布于 2025-02-01 11:58:54 字数 1752 浏览 1 评论 0原文

我实施了一些测试来检查某些页面的状态代码,但是这个带有反向函数的一个测试给我带来了错误: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢你一世 2025-02-08 11:58:54

由于您在urls.py中使用app_name =…,因此您需要以视图的名义将其指定为名称空间,因此ads : ad_detail,并指定一个主键:

resp = self.client.get(reverse('ads:ad_detail', kwargs={'pk': 42}))

因此,在这里,我们访问42的URL用作pk url参数的值。

Since you use an app_name=… in your urls.py, you need to specify this as a namespace in the name of the view, so ads:ad_detail, and specify a primary key:

resp = self.client.get(reverse('ads:ad_detail', kwargs={'pk': 42}))

So here we visit the URL where 42 is used as value for the pk URL parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文