django 2.0.7 NoReverseMatch at / posts

时间:2022-11-02 23:41:51

Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name.

反向找不到'post_detail'。 'post_detail'不是有效的视图函数或模式名称。

After Django goes to 2.0.7, I set up some url rules. It works locally at 127.0.0.1 on my computer, but not remotely on the machine (ubuntu 1604).

在Django进入2.0.7之后,我设置了一些网址规则。它在我的计算机上以127.0.0.1本地工作,但不能在机器上远程工作(ubuntu 1604)。

code: post_list.html:

代码: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 %}

blog.urls:

blog.urls:

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'),
]

blog.views:

blog.views:

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

mysite.urls:

mysite.urls:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]

Solved. I need to add 'blog' as my app_name in blog.url and add 'blog' as my namespace in mysite.url

解决了。我需要在blog.url中添加'blog'作为我的app_name,并在mysite.url中添加'blog'作为我的命名空间

Thanks guy!

哥们,谢啦!

In 2.0.7, I need to add an app_name (blog) in blog.url and add a namespace in mysite.url (blog). Only adding a namespace in mysite.url without adding an app_name in blog.url -> django will tell me to add an app name. Only adding an app_name in blog.url without adding a namespace in mysite.url -> django will tell me blog is a not registered namespace. Also remember to refresh gunicorn.

在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。

1 个解决方案

#1


0  

Suppose your app name is blog so in your blog's urls.py add

假设您的应用名称是博客,所以在您的博客的urls.py中添加

app_name= 'blog'

and in template use {% url 'blog:post_detail' pk=post.pk %}

并在模板中使用{%url'博客:post_detail'pk = post.pk%}

#1


0  

Suppose your app name is blog so in your blog's urls.py add

假设您的应用名称是博客,所以在您的博客的urls.py中添加

app_name= 'blog'

and in template use {% url 'blog:post_detail' pk=post.pk %}

并在模板中使用{%url'博客:post_detail'pk = post.pk%}