我正在尝试在我的 django 电子商务网站中创建一个新的 URL 级别,这意味着如果用户在domain.com/category/foo/
我试图添加下一个级别,他们点击 foo 中的某个元素并结束在domain.com/category/foo/tag/bar/
. 为此,我创建了我的 urls.py 行来检测这一点,我相信这里没有问题:
(r'^category/(?P<category_slug>[-\w]+)/tag/(?P<tag_slug>[-\w]+)/$', 'show_tag', {
'template_name':'catalog/product_list.html'},'catalog_tag'),
通过 urls.py 映射请求后,我知道它需要 view.py 中的一些变量get_adsolute_url
才能完成它的工作,所以我创建了视图:
def show_tag(request,
tag_slug,
template_name="catalog/product_list.html"):
t = get_object_or_404(Tag,
slug=tag_slug)
products = t.product_set.all()
page_title = t.name
meta_keywords = t.meta_keywords
meta_description = t.meta_description
return render_to_response(template_name,
locals(),
context_instance=RequestContext(request))
最后,在我的 Tag 模型中,我设置了 myget_absolute_url
来填充关键字参数:
@models.permalink
def get_absolute_url(self):
return ('catalog_tag', (), { 'category_slug': self.slug, 'tag_slug': self.slug })
我确定我要请求的类别和标签存在,然后我输入 domain.com/category/foo/tag/bar 并收到一个
TypeError at /category/foo/tag/bar/
show_tag() got an unexpected keyword argument 'category_slug'
我相信我知道错误在哪里,但我不知道如何解决它。我的get_abolute_url
集合'category_slug': self.slug
,但正如我所说,这个定义存在于我的标签模型中。我的 Category 模型位于同一个 models.py 中,但我如何告诉我get_absolute_url
去查找它?