Django 1.8没有反向匹配错误

时间:2015-06-09 20:58:00

标签: python django

每次点击template.html中的链接时,我都会收到以下错误消息http://prntscr.com/7f3l4d。有人可以帮我解决这个问题。

urls.py项目

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

urls.py app

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

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'

template.html

{% extends "index.html" %}
{% block mainmast %}
<div id="wrapper">
{% if feats %}
   {% for feat in feats %}
     <div class="specialsticky">
     <a href="{% url '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 %}

当我点击template.html中的图片时,会发生错误。谢谢。

1 个答案:

答案 0 :(得分:6)

您已将应用的网址放在命名空间feature中,因此在引用该网址时,您必须使用命名空间。

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

将您的模板更改为:<a href="{% url 'feature:detail' feat.id %}">,它会起作用。

https://docs.djangoproject.com/en/1.8/topics/http/urls/#url-namespaces