简体   繁体   中英

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. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$']

from the line 7 in home.html template :

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

From 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

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

From 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

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

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. However, it is well defined in 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. 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]+)/$'] 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: ('', ) . In the quotations there should be the ID, a number.

You're not being consistent in your home.html template on whether the variable is post or posts . Pick one - post makes more sense - and stick to it.

{% 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM