在网址格式中使用include()时出现意外的NoReverseMatch错误 [英] Unexpected NoReverseMatch error when using include() in urls patterns

查看:89
本文介绍了在网址格式中使用include()时出现意外的NoReverseMatch错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

index.html

未找到带有参数'(3,)'和关键字参数'{}'的'detail'。尝试了1个模式:['$(?P< pk> [0-9] +)/ $']

views.py

def rock_and_feat(request):
    feats = Feat.objects.order_by('-created')[:3]
    rocks = Rockinfo.objects.order_by('-rank')[:50]
    context = RequestContext(request, {
        'feats': feats, 'rocks': rocks
    })
    return render_to_response('template.html', context)


class DetailView(generic.DetailView):
    model = Feat
    template_name = 'feature/detail.html' 
    context_object_name = 'feat'

urls.py

urlpatterns = [
    url(r'^$', views.rock_and_feat, name='rock_and_feat'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]

index.html >

{% extends "index.html" %}
{% block mainmast %}
<div id="wrapper">
{% if feats %}
{% for feat in feats %}
 <div class="specialsticky">
 <a href="{% url 'feature:detail' feat.id %}"><img src="{{ feat.image.url }}" alt="some text"></a>
  <h1 class="mast-header">
    <a href="#">{{feat.title}}</a>
  </h1>
 </div>

 {% endfor %}
 {% else %}
<p>No </p>
 {% endif %}
</div>
{% endblock %}

detail.html

{% extends "index.html" %}

<iframe width="560" height="345" src="{{ feat.youtube_link }}"       frameborder="0" allowfullscreen></iframe>

项目 urls.py

project urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    url(r'^$', include('feature.urls', namespace="feature")),
    url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在添加<在 index.html 中图像上的href =

the app worked fine before I added the <a href= on the image in index.html.

推荐答案

这表明问题所在。

'$(?P<pk>[0-9]+)/$'

在模式的开头不应有美​​元符号(与字符串的末尾匹配)。

There shouldn't be a dollar sign (which matches the end of the string) at the beginning of the pattern.

问题是由方式ou包括urls.py。您当前在正则表达式中有一个美元:

The problem is caused by the way you are including the urls.py. You currently have a dollar in the regex:

url(r'^$', include('feature.urls', namespace="feature")),

要解决此问题,请从正则表达式中删除美元。

To fix the problem, remove the dollar from the regex.

url(r'^', include('feature.urls', namespace="feature")),

这篇关于在网址格式中使用include()时出现意外的NoReverseMatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆