繁体   English   中英

Django反向错误:NoReverseMatch

[英]Django reverse error: NoReverseMatch

我看了很多不同的帖子,但是他们都在使用不同版本的django,或者似乎没有用。 这是我正在尝试做的事情:

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(特定于应用程序):

urlpatterns = patterns ('' ,
url(r'^$', views.index, name='index'),

url(r'^(?P<slug>[\w\-]+)/$', views.posts, name="postdetail"),

)

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})

最后是模板:

 {% 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 %}

出于某种原因,这给了我一个“没有反向匹配”:反向'postdetail',带有参数'()'和关键字参数'{u'slug':u'third'}'找不到。 尝试过0种模式:[]

我已经尝试在模板中postdetail周围的双引号,并且我也尝试通过视图名称而不是模式名称来引用它。 仍然没有运气。 文档也没有太大帮助。

非常感谢帮助! 谢谢

您在包含URL时使用了命名空间,因此您可能需要使用"blog:postdetail"来反转它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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