django cms - 用slug解析页面url

时间:2013-04-10 10:43:11

标签: django django-views django-cms

是否有可能通过其slug解析页面网址? 类似的东西:

from django.core.urlresolvers import reverse
reverse('pages-details-by-slug',{slug:'my-page-slug'})

但我得到了:

NameError: name 'slug' is not defined

对此有何帮助?

2 个答案:

答案 0 :(得分:6)

Python不是Javascript;字典键需要引用。

reverse('pages-details-by-slug', {'slug': 'my-page-slug'})

答案 1 :(得分:1)

解决:

try:
    from cms.models import Title
    page = Title.objects.get(slug="my-page-slug").page
    return page.get_absolute_url()
except Title.DoesNotExist:
    pass