简体   繁体   English

Django反向错误:NoReverseMatch

[英]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.

相关问题 Django反向错误:NoReverseMatch位于 - Django Reverse Error: NoReverseMatch at Django:获取错误NoReverseMatch进行反向 - Django: getting error NoReverseMatch for reverse Django NoReverseMatch错误,反向函数不带参数不起作用 - Django NoReverseMatch error, reverse function not work with no arguments Django HttpResponseRedirect(reverse(&#39;url&#39;))返回NoReverseMatch错误 - Django HttpResponseRedirect(reverse('url')) Returns NoReverseMatch Error django DeleteView 处的反向惰性错误 NoReverseMatch - reverse lazy error NoReverseMatch at django DeleteView django 错误:NoReverseMatch at /watchlist/ Reverse for &#39;viewList&#39; with arguments &#39;(&#39;&#39;,)&#39; - django error : NoReverseMatch at /watchlist/ Reverse for 'viewList' with arguments '('',)' 适用于DetailView的Django NoReverseMatch反向 - Django NoReverseMatch Reverse for DetailView NoReverseMatch 错误。 未找到“...”的反转 - NoReverseMatch error . Reverse for '…' not found Django 错误:NoReverseMatch at /Post/8 Reverse for &#39;comments&#39; with arguments &#39;(&#39;&#39;,)&#39; not found - Django error : NoReverseMatch at /Post/8 Reverse for 'comments' with arguments '('',)' not found Python Django错误NoReverseMatch异常值:反向为&#39;updatecontact&#39;,未找到参数 - Python Django error NoReverseMatch Exception Value: Reverse for 'updatecontact' with no arguments not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM