让 @permalink 装饰器与 django 通用视图一起使用?
也许我遗漏了一些东西,但根据 django 文档(1.2),我已经完全按照指定设置了我的 URLS 模型,以确保我没有对 get_absolute_url 返回的 URL 进行硬编码。
这就是我所拥有的:
在 urls.py 中
urlpatterns = patterns('django.views.generic.list_detail',
url(r'^$','object_list',
{ 'queryset': product.objects.all(),
'template_name': 'products/list.html',
},
name='product_list'),
url(r'^(?P<slug>[-\w]+)/$','object_detail',
{ 'queryset': product.objects.all(),
'template_name': 'products/detail.html',
},
name='product_detail'),
)
在 models.py 中
@models.permalink
def get_absolute_url(self):
return ('product_detail', (), {'slug': str(self.slug)})
该方法在模板中返回一个空字符串,并且从 shell 中给出一个错误。
NoReverseMatch: Reverse for 'product_detail' with arguments '()' and keyword arguments '{'slug': 'dd-d--'}' not found.
这应该可以解决,因为 urls.py 有一个名称:product_detail?
Maybe I am missing something, but according to the django docs (1.2), I have setup my URLS models exactly as specified to ensure I am not hard-coding urls returned for get_absolute_url.
Here's what I have:
in urls.py
urlpatterns = patterns('django.views.generic.list_detail',
url(r'^
in models.py
@models.permalink
def get_absolute_url(self):
return ('product_detail', (), {'slug': str(self.slug)})
The method returns an empty string in the templates, and from the shell it gives an error.
NoReverseMatch: Reverse for 'product_detail' with arguments '()' and keyword arguments '{'slug': 'dd-d--'}' not found.
This should resolve should it not, since urls.py has a name : product_detail?
,'object_list',
{ 'queryset': product.objects.all(),
'template_name': 'products/list.html',
},
name='product_list'),
url(r'^(?P<slug>[-\w]+)/
in models.py
The method returns an empty string in the templates, and from the shell it gives an error.
This should resolve should it not, since urls.py has a name : product_detail?
,'object_detail',
{ 'queryset': product.objects.all(),
'template_name': 'products/detail.html',
},
name='product_detail'),
)
in models.py
The method returns an empty string in the templates, and from the shell it gives an error.
This should resolve should it not, since urls.py has a name : product_detail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
语法似乎是正确的,您确定您的 urls.py 已包含在内吗?尝试在视图代码中单步执行 debuggin 并首先使用 reverse 函数生成 url。
我的盲目猜测是,您的 urls.py 文件总体上有问题。
Syntax seems to be correct, are you sure your urls.py gets included? Try stepping in debuggin in view code and use reverse function first to generate the url.
My blind guess would be, something is wrong with your urls.py file in general.
尝试更改此行:
Carret (
,'object_detail',^
) 代表行的开头,因此在您编写它的上下文中这是不合逻辑的,因为这意味着该行甚至在开始之前就有内容。为
Carret (
^
) 代表行的开头,因此在您编写它的上下文中这是不合逻辑的,因为这意味着该行甚至在开始之前就有内容。Try changing this line:
Carret (
,'object_detail',^
) stands for beginning of the line, so it is illogical in the context you wrote it since it means the line has content before it even begins.to
Carret (
^
) stands for beginning of the line, so it is illogical in the context you wrote it since it means the line has content before it even begins.