0

我正在尝试使用 Django 开发博客脚本。但是当我想显示帖子链接时,出现 NoReverseMatch 错误。

我的观点.py

# -*- coding: utf-8 -*-

# Create your views here.

from .models import Yazi, Yorum, Kategori
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.template import RequestContext, loader
from django.contrib.sites.models import Site

def Home(request):
    try:
        posts = Yazi.objects.filter(yayinlanmis=True).order_by('-yayinlanma_tarihi')
    except Yazi.DoesNotExist:
        raise Http404
    site = Site.objects.get_current()

    c = RequestContext(request,{
        'posts':posts,
        'site':site
        })
    return render_to_response('Penguencik_Yazilar/yazi_list.html', c)

def Detail(request, slug):
    post = Yazi.objects.get(slug=slug)
    site = Site.objects.get_current()

    c= RequestContext(request,{
        'posts':post,
        'site':site
        })
    return render_to_response('Penguencik_Yazilar/yazi_detail.html',c)

应用程序文件夹中的 Urls.py。

from django.conf.urls import patterns, url
import views

urlpatterns = patterns('',
    url(r'^$', views.Home,name='index'),
    url(r'^/makale/(?P<slug>[0-9A-Za-z._%+-]+)$', views.Detail,name='detail'),
)

项目文件夹中的 Urls.py

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', include('Penguencik_Yazilar.urls',namespace='blog')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

和模板代码。我究竟做错了什么?

{% load url from future %}
...
<a href="{% url 'blog:detail' yazi.slug %}">Read</a>

错误

4

1 回答 1

0

尝试改变这一点:

<a href="{% url 'blog:detail' yazi.slug %}">Read</a>

至:

<a href="{% url 'blog:detail' slug=yazi.slug %}">Read</a>

导致您的视图slug在这里期望关键字(?P<slug>[0-9A-Za-z._%+-]+)

于 2013-07-25T11:42:53.530 回答