[英]Django reverse error: NoReverseMatch
I've looked at a lot of different posts, but they're all either working with a different version of django or don't seem to work. 我看了很多不同的帖子,但是他们都在使用不同版本的django,或者似乎没有用。 Here is what I'm trying to do: 这是我正在尝试做的事情:
urls.py (for the entire project): urls.py(适用于整个项目):
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
)
urls.py (specific to the app): urls.py(特定于应用程序):
urlpatterns = patterns ('' ,
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>[\w\-]+)/$', views.posts, name="postdetail"),
)
views.py: views.py:
def index(request):
posts = Post.objects.filter(published=True)
return render(request,'blog/index.html',{'posts':posts})
def posts(request, slug):
post = get_object_or_404(Post,slug=slug)
return render(request, 'blog/post.html',{'post':post})
And finally the template: 最后是模板:
{% block title %} Blog Archive {% endblock %}
{% block content %}
<h1> My Blog Archive </h1>
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url "postdetail" slug=post.slug %}">
{{post.title}}
</a>
</h2>
<p>{{post.description}}</p>
<p>
Posted on
<time datetime="{{post.created|date:"c"}}">
{{post.created|date}}
</time>
</p>
</div>
{% endfor %}
{% endblock %}
For some reason this gives me a "No reverse Match": Reverse for 'postdetail' with arguments '()' and keyword arguments '{u'slug': u'third'}' not found. 出于某种原因,这给了我一个“没有反向匹配”:反向'postdetail',带有参数'()'和关键字参数'{u'slug':u'third'}'找不到。 0 pattern(s) tried: [] 尝试过0种模式:[]
I've already tried getting rid of the double quotes around postdetail
in the template, and I've also tried referring to it by the view name instead of the pattern name. 我已经尝试在模板中postdetail
周围的双引号,并且我也尝试通过视图名称而不是模式名称来引用它。 Still no luck. 仍然没有运气。 The documentation isn't too helpful either. 文档也没有太大帮助。
Help is really appreciated! 非常感谢帮助! Thanks 谢谢
您在包含URL时使用了命名空间,因此您可能需要使用"blog:postdetail"
来反转它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.