未找到“post_detail”的反向内容。 “post_detail”不是有效的 View 函数或模式名称。
Django到2.0.7后,我设置了一些url规则。它可以在我的计算机上本地运行,地址为 127.0.0.1,但不能在远程计算机(ubuntu 1604)上运行。
代码: post_list.html:
{% for post in posts %}
<div>
<p>published: {{ post.published_date }}</p>
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endfor %}
博客.url:
urlpatterns = [
path('', views.home_page, name='home_page'),
path('posts', views.post_list, name='post_list'),
path('post/<pk>/', views.post_detail, name='post_detail'),
]
博客浏览量:
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
我的网站.url:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
已解决。我需要在 blog.url 中添加“blog”作为我的 app_name,并在 mysite.url 中添加“blog”作为我的命名空间
谢谢大佬!
在2.0.7中,我需要在blog.url中添加app_name(博客),并在mysite.url(博客)中添加命名空间。仅在 mysite.url 中添加命名空间而不在 blog.url -> django 中添加 app_name 会告诉我添加应用程序名称。仅在 blog.url 中添加 app_name 而不在 mysite.url -> django 中添加命名空间会告诉我 blog 是未注册的命名空间。另外记得刷新gunicorn。
最佳答案
假设您的应用名称是 blog,因此在您博客的 urls.py 中添加
app_name= 'blog'
并在模板中使用{% url 'blog:post_detail' pk=post.pk %}
关于python - django 2.0.7 NoReverseMatch 在/帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51544101/