是否有可能通过其slug解析页面网址? 类似的东西:
from django.core.urlresolvers import reverse
reverse('pages-details-by-slug',{slug:'my-page-slug'})
但我得到了:
NameError: name 'slug' is not defined
对此有何帮助?
答案 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