【发布时间】:2013-04-02 17:05:35
【问题描述】:
我正在尝试在我的 django 电子商务网站中创建一个新的 URL 级别,这意味着如果用户在 domain.com/category/foo/ 中,我正在尝试添加下一个级别,他们在其中单击 foo 和 wind 中的某些元素在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 映射请求后,我知道它需要 views.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 模型中,我设置了 get_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 去查找它?
【问题讨论】:
标签: django url django-views