django - 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"> Testimonies</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})
解决方案
模板中的 url 部分是错误的。必须是:{% url 'Testimony:testimonypost' %}
使用命名空间后,您必须在 url 中使用应用名称。
看到这个:https ://docs.djangoproject.com/en/2.1/topics/http/urls/#reversing-namespaced-urls
推荐阅读
- ruby-on-rails-4 - rails4 将默认 url xxx.com/new_page 更改为 xxx.com/new_page/username
- haskell - Haskell,使用用户输入创建列表
- php - 将 curl 翻译成 CakePHP 3.6 httpclient?
- ruby - 在 ruby 上解析日志文件
- printing - Gtk:我如何预设/恢复选定的打印机?
- git - IntelliJ Idea gpg 未能签署数据
- ios - 将动画添加到视图
- distributed-computing - 我可以使用 Mesos 预留间接资源吗?
- javascript - 当我尝试将 google place 自动完成 API 与 Axios 一起使用时出现 CORS 错误
- javascript - 如何使用 node .js 将 html 的值表添加到 mysql