Django 1.7错误NoReverseMatch at' / post /'

时间:2015-02-20 04:51:54

标签: python django

我是django的新手。 我在下面遇到错误消息异常值。

Reverse for 'category_detail' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['category/(?P<pk>[0-9]+)/$']
模板渲染期间出现

错误

Reverse for 'category_detail' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['category/(?P<pk>[0-9]+)/$']

排队:

 <li><a href="{% url 'category_detail' pk=category.pk %}/">{{ cat.title }}</a></li>

djangogirls / APSI / urls.py

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('blog.urls')),
)

djangogirls /博客/ urls.py

from django.conf.urls import patterns, include, url
来自。导入视图

urlpatterns = patterns('',
        url(r'^post/$', views.post_list),
        url(r'^category/(?P<pk>[0-9]+)/$', views.category_detail, name='category_detail'),
        url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
        url(r'^post/new/$', views.post_new, name='post_new'),
        url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),

)

djangogirls /博客/ views.py

def category_detail(request, pk):
category = Post.objects.filter(category__id=pk)
return render(request, 'blog/category_detail.html', {'category': category})


def post_list(request):
    posts       = Post.objects.filter(published_date__isnull=False).order_by('published_date')
    categories  = Category.objects.all()
    tags        = Tag.objects.all()
    return render(request, 'blog/post_list.html', {'posts': posts, 'categories': categories, 'tags':tags })

djangogirls /博客/ models.py

class Category(models.Model):

    title           = models.CharField(max_length=50)
    slug            = models.CharField(max_length=200)
    description     = models.TextField()
    created_date    = models.DateTimeField(auto_now_add=True)
    published_date  = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

class Post(models.Model):

    author          = models.ForeignKey('auth.User')
    category        = models.ForeignKey('Category')
    title           = models.CharField(max_length=200)
    text            = models.TextField()
    created_date    = models.DateTimeField(auto_now_add=True)
    published_date  = models.DateTimeField(blank=True, null=True)
    likes           = models.IntegerField(default=0)
    thumbnail       = models.FileField(upload_to = get_upload_file_name)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title
你可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

urls.py中参数的名称为pk

url(r'^category/(?P<pk>[0-9]+)/$', views.category_detail, name='category'),

但您正在尝试使用参数category_id查找网址。将其更改为:

{% url 'blog.views.category_detail' pk=category.pk %}

甚至省略参数名称:

{% url 'blog.views.category_detail' category.pk %}

而且imho,就您的网址有name而言,最好通过它的名称而不是查看名称来查找此网址:

{% url 'category' category.pk %}

更新:在post_list.html中您使用此代码:

<a href="{% url 'category_detail' pk=category.pk %}/">{{ cat.title }}</a>

据我所知,类别变量名为cat。 Yous应该将正确的变量传递给{% url %}标记:

<a href="{% url 'category_detail' pk=cat.pk %}">{{ cat.title }}</a>

要解决此类问题,请务必考虑在Category模型中实施get_absolute_url()方法。只要您需要指向类别,请使用此方法:

<a href="{{ cat.get_absolute_url }}">{{ cat.title }}</a>

答案 1 :(得分:1)

查看您的变量名称,您使用cat获取标题,使用category获取pk。也许category未定义?这将导致在url标记中插入一个空字符串,并导致收到的错误消息。