简体   繁体   English

使用参数 '('',)' 和关键字参数 '{}' 未找到的 'post_detail' 反转。 尝试了 1 个模式:['posts/(?P[0-9]+)/$']

[英]Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$']

I have this error :我有这个错误:

Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found.使用参数 '('',)' 和关键字参数 '{}' 未找到的 'post_detail' 反转。 1 pattern(s) tried: ['posts/(?P[0-9]+)/$']尝试了 1 个模式:['posts/(?P[0-9]+)/$']

from the line 7 in home.html template :home.html模板的第 7 行开始:

<a href="{% url 'post_detail' post.id %}">{{ post.title }}</a>

From home.html来自 home.html

<h1>Welcome to Jeremie's blog</h1>

<h2>Latest Posts</h2>

{% for posts in posts.all %}

<a href="{% url 'post_detail' post.id %}">{{ post.title }}</a>
<br />
{{ posts.pub_date_pretty }}
<br />
<img src = "{{ post.image.url }}" />
<br />
{{ posts.summary }}
<br />
<br />

{% endfor %}

From posts_details.html来自posts_details.html

{{ post.title }}
<br />
{{ posts.pub_date_pretty}}
<br />
<img src = "{{ post.image.url }}" />
<br />
{{posts.summary}}
<br />
<br />

From urls.py来自 urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home),
    url(r'^posts/(?P<post_id>[0-9]+)/$', views.post_details, name="post_detail"),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

From views.py来自views.py

def home(request):
    posts = Post.objects.order_by('pub_date')
    return render(request, 'posts/home.html', {'posts':posts})

def post_details(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    return render(request, 'posts/posts_detail.html', {'post':post})

From models.py来自models.py

class Post(models.Model):
    title = models.CharField(max_length=255)
    pub_date = models.DateTimeField()
    image = models.ImageField(upload_to='media')
    body = models.TextField()

    def __str__(self):
        return self.title

    def pub_date_pretty(self):
        return self.pub_date.strftime('%b %e %Y')

    def summary(self):
        return self.body[:100]

It seems that post.id isn't available in the template.模板中似乎没有post.id However, it is well defined in views.py .然而,它在views.py有很好的定义。

Could anyone be able to tell me how could I fix this problem?谁能告诉我如何解决这个问题?

It seems that you don't have post.id available in the template, probably because the post isn't created yet.模板中似乎没有post.id可用,可能是因为尚未创建帖子。 If I don't know your logic patterns, I can't tell you exactly why the post.id isn't there, but you see that post.id is an empty string from the error message: Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$']如果我不知道你的逻辑模式,我不能确切地告诉你为什么post.id不存在,但是你看到post.id是错误消息中的一个空字符串: Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$'] Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$'] where the arguments is a tuple of one item that is an empty string, namely this: ('', ) . Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$']其中参数是一个空字符串项的元组,即: ('', ) In the quotations there should be the ID, a number.在引文中应该有 ID,一个数字。

You're not being consistent in your home.html template on whether the variable is post or posts .您在 home.html 模板中关于变量是post还是posts不一致。 Pick one - post makes more sense - and stick to it.选择一个 - post更有意义 - 并坚持下去。

{% for post in posts.all %}

<a href="{% url 'post_detail' post.id %}">{{ post.title }}</a>
{{ post.pub_date_pretty }}
<img src = "{{ post.image.url }}" />
{{ post.summary }}

{% endfor %}

(Also, not related but note that it's extremely poor practice to use br tags like that. Please use proper semantic markup and use CSS to manage the space between elements.) (另外,不相关,但请注意,使用这样的br标签是非常糟糕的做法。请使用适当的语义标记并使用 CSS 来管理元素之间的空间。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决此问题: 使用关键字 arguments '{'slug': ''}' 反向查找 'post_detail'。 尝试了 1 种模式:['(?P<slug> [-a-zA-Z0-9_]+)/$']?</slug> - How to fix this: Reverse for 'post_detail' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['(?P<slug>[-a-zA-Z0-9_]+)/$']? 未找到带有参数 &#39;(&#39;&#39;,)&#39; 的 &#39;post_detail&#39; 反转。 尝试了 1 个模式:[&#39;post/(?P<slug> [-\\\\w]+)/$&#39;] - Reverse for 'post_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<slug>[-\\w]+)/$'] 未找到参数为 &#39;(&#39;&#39;,)&#39; 的 &#39;post-detail&#39; 的 Django Reverse。 尝试了 1 个模式:[&#39;post\\\\/(?P<pk> [0-9]+)\\\\/$&#39;] - Django Reverse for 'post-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post\\/(?P<pk>[0-9]+)\\/$'] 如何修复 NoReverseMatch at / Reverse for &#39;post_detail&#39; 关键字参数 &#39;{u&#39;pk&#39;: &#39;&#39;}&#39; not found。 尝试了 1 个模式:[&#39;post/<int:pk> /&#39;] - How to fix NoReverseMatch at / Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['post/<int:pk>/'] 找不到带有关键字参数&#39;{&#39;id&#39;:5}&#39;的&#39;detail&#39;。 尝试了1个模式:[&#39;posts /(?P <slug> [\\\\瓦特 - ] +)/ $&#39;] - Reverse for 'detail' with keyword arguments '{'id': 5}' not found. 1 pattern(s) tried: ['posts/(?P<slug>[\\w-]+)/$'] 找不到带有关键字参数&#39;{&#39;pk&#39;:&#39;&#39;}&#39;的&#39;ques_detail&#39;。 尝试了1个模式:[&#39;ques_detail /(?P <pk> [0-9] +)/ $&#39;] - Reverse for 'ques_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['ques_detail/(?P<pk>[0-9]+)/$'] 找不到带有参数“(&#39;&#39;,)”的“ news_detail”。 尝试了1个模式:[&#39;news \\\\-(?P <news_pk> [0-9] +)$&#39;] - Reverse for 'news_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['news\\-(?P<news_pk>[0-9]+)$'] .NoReverseMatch:未找到参数为 &#39;(&#39;&#39;,)&#39; 的 &#39;author-detail&#39; 反转。 尝试了 1 个模式:[&#39;目录/作者/(?P<pk> [0-9]+)$&#39;] - .NoReverseMatch: Reverse for 'author-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/author/(?P<pk>[0-9]+)$'] 未找到 arguments '('',)' 的 'book-detail' 反向。 尝试了 1 种模式:['catalog/book/(?P<pk> [0-9]+)$']</pk> - Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/book/(?P<pk>[0-9]+)$'] 未找到 arguments '('',)' 的 'article_detail' 反向。 尝试了 1 种模式:['article/(?P<pk> [0-9]+)$']</pk> - Reverse for 'article_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['article/(?P<pk>[0-9]+)$']
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM