我试图学习django而且我遇到了问题。
Reverse for 'post_detail' with arguments '()' and keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/$']
我搜索了同样的问题,但解决方案并不适用于我。 也许这很重要 - 我遵循本教程 http://tutorial.djangogirls.org/en/extend_your_application/index.html 谢谢!
views.py
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, 'blog/post_list.html', {'posts': posts})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
base.html文件
{% load staticfiles %}
<html>
<head>
<title>Django Girls blog</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 href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
<div class="page-header">
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
</div>
<div class="content container">
<div class="row">
<div class="col-md-8">
{% block content %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
urls.py
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
]
追踪错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.1
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Template error:
In template D:\Projects\mysite\blog\templates\blog\base.html, error at line 13
Reverse for 'post_detail' with arguments '()' and keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/$']
3 : <head>
4 : <title>Django Girls blog</title>
5 : <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
6 : <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
7 : <link href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
8 : <link rel="stylesheet" href="{% static 'css/blog.css' %}">
9 : </head>
10 : <body>
11 :
12 : <div class="page-header">
13 : <h1><a href=" {% url 'post_detail' pk=post.pk %} ">{{ post.title }}</a></h1>
14 : </div>
15 : <div class="content container">
16 : <div class="row">
17 : <div class="col-md-8">
18 : {% block content %}
19 : {% endblock %}
20 : </div>
21 : </div>
22 : </div>
23 :
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Projects\mysite\blog\views.py" in post_list
8. return render(request, 'blog/post_list.html', {'posts': posts})
File "C:\Python34\lib\site-packages\django\shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py" in render_to_string
99. return template.render(context, request)
File "C:\Python34\lib\site-packages\django\template\backends\django.py" in render
74. return self.template.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
209. return self._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
507. six.reraise(*exc_info)
File "C:\Python34\lib\site-packages\django\utils\six.py" in reraise
659. raise value
File "C:\Python34\lib\site-packages\django\template\defaulttags.py" in render
493. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py" in reverse
579. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
496. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'post_detail' with arguments '()' and keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/$']
答案 0 :(得分:1)
问题是:
{% url 'post_detail' pk=post.pk %}
在base.html
模板的该范围内,没有post
变量,因此post.pk
解析为空字符串。那么Django试图用空的pk
进行反向反向,但失败了。
要使{% url %}
有效,您需要在其中添加帖子。
答案 1 :(得分:1)
您似乎错过了教程中的一篇文章:http://tutorial.djangogirls.org/en/extend_your_application/index.html#create-a-template-link-to-a-posts-detail
在该步骤中,它会指示您创建新模板,并将创建问题的行<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
添加到该模板中。
该行尤其不适用于您的base.html,因为没有名为post
的变量。该变量是在本教程的该步骤中作为新模板中循环的一部分创建的。
答案 2 :(得分:1)
我认为您的问题是,您尝试访问单个post
而不迭代posts
。
正如您在post_list
视图中看到的那样,您有:
...
return render(request, 'blog/post_list.html', {'posts': posts})
因此,您将名为posts
的变量发送到模板。
但是在您的模板中,您尝试访问post
:
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
但它不存在。
您应该遍历posts
,例如:
{% for post in posts %}
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
{% endfor %}
这应该有效。
答案 3 :(得分:0)
我有同样的问题。只需替换为:
return redirect('blog.views.post_detail', pk=post.pk)