让 @permalink 装饰器与 django 通用视图一起使用?

发布于 2024-09-27 06:44:48 字数 1020 浏览 3 评论 0原文

也许我遗漏了一些东西,但根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就像说晚安 2024-10-04 06:44:48

语法似乎是正确的,您确定您的 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.

寄居者 2024-10-04 06:44:48

尝试更改此行:

url(r'(?P<slug>[-\w]+)/^

url(r'^(?P<slug>[-\w]+)/

Carret (^) 代表行的开头,因此在您编写它的上下文中这是不合逻辑的,因为这意味着该行甚至在开始之前就有内容。

,'object_detail',


Carret (^) 代表行的开头,因此在您编写它的上下文中这是不合逻辑的,因为这意味着该行甚至在开始之前就有内容。

,'object_detail',

Carret (^) 代表行的开头,因此在您编写它的上下文中这是不合逻辑的,因为这意味着该行甚至在开始之前就有内容。

,'object_detail',

Carret (^) 代表行的开头,因此在您编写它的上下文中这是不合逻辑的,因为这意味着该行甚至在开始之前就有内容。

Try changing this line:

url(r'(?P<slug>[-\w]+)/^

to

url(r'^(?P<slug>[-\w]+)/

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.

,'object_detail',

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.

,'object_detail',

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.

,'object_detail',

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文