首页 > 解决方案 > NoReverseMatch:未找到“testimonypost”的反向。'testimonypost' 不是有效的视图函数或模式名称

问题描述

我想不通。我尝试了不同的方法无济于事。在我的 urls.py 中注释掉 app_name 可以让我完美地进入页面。关于 app_name 有什么我应该知道的吗?有任何想法吗?views.py 附在下面。任何帮助表示赞赏。谢谢!

 #models.py
    class Testimony(models.Model):
        title = models.CharField(max_length=100)

        def get_absolute_url(self):
           return reverse("Testimony:details", kwargs={"id": self.id} )

    #urls.py
    app_name='Testimony'
    urlpatterns=[
    path('testimonypost/', views.TestimonyOrderView, name='testimonypost'),]


    #template(traceback points to this line)
    <li><a href="{% url 'testimonypost'%}" class="glyphicon glyphicon-grain">&nbspTestimonies</a></li>

#views.py
def TestimonyOrderView(request):
    queryset_list=Testimony.objects.annotate().order_by('timestamp')

    paginator = Paginator(queryset_list, 20)
    page_request_var="page"
    page=request.GET.get(page_request_var)
    try:
        queryset=paginator.page(page)
    except PageNotAnInteger:
            queryset=paginator.page(1)
    except EmptyPage:
        queryset=paginator.page(paginator_num.pages)


    return render(request, 'testimony_post.html', {'queryset_list':queryset_list})

标签: django

解决方案


模板中的 url 部分是错误的。必须是:{% url 'Testimony:testimonypost' %} 使用命名空间后,您必须在 url 中使用应用名称。

看到这个:https ://docs.djangoproject.com/en/2.1/topics/http/urls/#reversing-namespaced-urls


推荐阅读