Getting @permalink decorator to work with django generic views?
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'^$','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'),
)
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?
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.
Try changing this line:
url(r'(?P<slug>[-\w]+)/^$','object_detail',
to
url(r'^(?P<slug>[-\w]+)/$','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.
精彩评论