[英]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>
<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 %}
{{ post.title }}
<br />
{{ posts.pub_date_pretty}}
<br />
<img src = "{{ post.image.url }}" />
<br />
{{posts.summary}}
<br />
<br />
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)
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})
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.