I have tried everything i found on the internet which was similar to my problem, but it did not help. Please help if you know the answer.
I get an ERROR while trying to set up my post_detail page of my blog.
The ERROR MESSAGE:
Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found.
1 pattern(s) tried: ['post/<int:pk>/']
My post_list.html
{% extends "blog/base.html" %}
{% load static %}
<html>
<head>
<title>Code Reminder</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
</head>
<body>
<div class="page-header">
<h1><a href="/">Coder Reminder</a></h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-4">
{% block content %}
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h1><a href="{% url 'post_detail' pk=post.blog.pk %}">{{ post.title }}</a></h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endfor %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
My post_detail.html
{% extends "blog/base.html" %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% endif %}
<h2>{{ post.title }}</h2>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endblock %}
My base.html
{% load static %}
<html>
<head>
<title>Code Reminder</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
</head>
<body>
<div class="page-header">
<h1><a href="/">Code Reminder</a></h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-8">
{% block content %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
my views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Create your views here.
from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404
def post_list(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, '/home/ud/PycharmProjects/blog/blog/blog/templates/blog/post_list.html', {'posts': posts})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, '/home/ud/PycharmProjects/blog/blog/blog/templates/blog/post_detail.html', {'post': post})
My urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url('', views.post_list, name='post_list'),
url('post/<int:pk>/', views.post_detail, name='post_detail'),
]
And finally my myblog/urls.py
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('', include('blog.urls')),
]
Please help me if you know the answer. I really tried everything but i cant find out my mistake.
Regards
The error message says
Reverse for 'post_detail' with keyword arguments '{u'pk': ''}' not found.
Which shows that the pk
is evaluating to the empty string ''
.
Your URL tag is
{% url 'post_detail' pk=post.blog.pk %}
That means you are trying to pass the post's blog's pk. But you almost certainly want the post's pk instead:
{% url 'post_detail' pk=post.pk %}
Secondly, you are using the old url()
with the path()
syntax (new in Django 2.0). Your urls should be:
from django.urls import path
urlpatterns = [
path('', views.post_list, name='post_list'),
path('post/<int:pk>/', views.post_detail, name='post_detail'),
]
The problem might be due to the older version of Python or Django. Try changing your My urls.py file as follow:
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
]
Here it will help to parse the address properly. If it still doesn't work the try to replace following in My post_list.html file
Old:
{% url 'post_detail' pk=post.blog.pk %}
New:
{% url 'post_detail' pk=post.pk %}
Hope this will help!
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.