文章URL中的类别slug(ManyToManyField)

时间:2015-02-04 20:46:28

标签: django url categories slug manytomanyfield

我正在尝试构建一个URL模式,例如/ category / article /

但问题是,一篇文章可能涉及很多类别,因此URL应该类似于/ category + category / article /

我有什么:

models.py

from django.db import models
from django.core.urlresolvers import reverse

class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    slug = models.SlugField(unique=True)
    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('view_category', kwargs={ 'slug': self.slug })

    class Meta:
        verbose_name_plural = "Categories"

class Work(models.Model):
    category = models.ManyToManyField(Category)
    title = models.CharField(max_length=128)        
    slug = models.SlugField(unique=True)
    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('view_work', kwargs={'slug': self.slug, 'category': self.category })

views.py

from django.shortcuts import render_to_response, get_object_or_404
from app.models import Work, Category

def index(request):
    return render_to_response('app/index.html', {
        'works': Work.objects.all()[:5]        
    })


def all_works(request):
    return render_to_response('app/all_works.html', {
        'categories': Category.objects.all(),
        'works': Work.objects.all()[:5]
    })


def view_category(request, slug):
    category = get_object_or_404(Category, slug=slug)
    return render_to_response('app/categories.html', {
        'categories': Category.objects.all(),
        'category': category,
        'works': Work.objects.filter(category=category)[:5]
    })

def view_work(request, slug, category):
    return render_to_response('app/work.html', {
        'work': get_object_or_404(Work, slug=slug, category=category)
    })

urls.py

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

urlpatterns = patterns('',
        url(r'^works/$', views.all_works, name='view_all_works'),
        url(r'^works/(?P<slug>[-\w]+)/$', views.view_category, name='view_category'),
        url(r'^work/(?P<category>[-\w]+)/(?P<slug>[-\w]+)/$', views.view_work, name='view_work'),
        url(r'^$', views.index, name='index'),
)
模板中的

<a href="{{ work.get_absolute_url|slice:'6:' }}">{{ work.title }}</a>

目前我收到了错误消息:

KeyError at /
u'category'
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.7.2
Exception Type: KeyError
Exception Value:    
u'category'
Exception Location: E:\web\env\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 448



11                  <li>
12                      <a href="{{ work.get_absolute_url|slice:'6:' }}" >{{ work.title }}</a>
13                  </li>

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

我认为这是因为Work.categoryManyToMany字段。因此,在您的Work.get_absolute_url函数中,您试图获得实际返回self.category的{​​{1}}(结果将返回django.db.models.fields.related.ManyRelatedManager的列表)。

您可以通过将absolute_url_function更改为:

来解决此问题
Category