Django - 我如何获得永久性使用“一次性”金属块 [英] Django - how can I get permalink to work with "throwaway" slug
问题描述
目前,我有一些网页可以使用一个网址,但是我的django应用程序中的URL很少,像这样:
http://example.com/foo/123/
我想添加'slugified'url,如下所示:
http://example.com/foo/123/foo-name-here
我可以让它工作正常,只需修改urlconf并在视图函数中添加一个不必要的值:
urls.py
ulpatterns = patterns('project.app.views',
url(r'^ foo /(?P< foo_id> \d +)/(?P< name_slug& ; \w +)/ $','foo_detail',name ='foo_detail'),
)
#views.py:
def foo_detail ,foo_id,name_slug):
#这里的东西,名字slug只是被丢弃
url与slug工程很好。
但是,我的问题是当我我使用 @ models.permalink
。
对于我的 Foo
模型,我以前有以下的工作很好:
@ models.permalink
def get_absolute_url自我):
return('foo_detail',[str(self.id),])
然而,在我的更改后,每当我在我的模板中调用 {{foo.get_absolute_url}}
,结果始终是一个空字符串。
我已经尝试了以下两个代替 get_absolute_url
,这两个都不起作用:
从django.template.defaultfilters导入slugify
#尝试1
@ models.permalink
def get_absolute_url(self):
return('foo_detail',[str(self.id),slugify(self.name)])
#attempt 2
@ models.permalink
def get_absolute_url self):
return('foo_detail',(),{
'foo_id':str(self.id),
'name_slug':slugify elf.name),
})
请注意,如果我添加 print slugify(self.name)
在返回之前,控制台中显示的标记名称正常。
调用 {{foo.get_absolute_url}}
在我的模板中,结果始终是一个空字符串,我没有收到任何错误。
$ b $我知道我可以用
return'/ foo /%s /%s'%(str(self.id),slugify(self.name))
,但我试图获得固定链接工作,以便我的URL只在一个地方定义。我做错了什么?有一件事要检查,因为我也遇到这个问题:
(?P< name_slug> \w +)
是否在任何地方添加连字符?如果是这样,正则表达式将不匹配,hypens是一个非字符的字符。修正使用 [\w - ] +
或类似的。
I'm trying to add slugs to the url in my django app, much like SO does.
Currently, I have pages that work just fine with a url like this:
http://example.com/foo/123/
I'd like to add 'slugified' urls like so:
http://example.com/foo/123/foo-name-here
I can get it to work just fine, by simply modifying the urlconf and adding a throwaway value to the view function:
#urls.py
ulpatterns = patterns('project.app.views',
url(r'^foo/(?P<foo_id>\d+)/(?P<name_slug>\w+)/$', 'foo_detail', name='foo_detail'),
)
#views.py:
def foo_detail(request, foo_id, name_slug):
# stuff here, name slug is just discarded
Visting the url with the slug works just fine.
However, my problem is when I am using @models.permalink
.
For my Foo
model, I used to have the following, which worked just fine:
@models.permalink
def get_absolute_url(self):
return ('foo_detail', [str(self.id),])
However, after my change, whenever I call {{ foo.get_absolute_url }}
in my templates, the result is always an empty string.
I have tried the following two replacements for get_absolute_url
, neither of which is working:
from django.template.defaultfilters import slugify
# attempt 1
@models.permalink
def get_absolute_url(self):
return ('foo_detail', [str(self.id), slugify(self.name)])
# attempt 2
@models.permalink
def get_absolute_url(self):
return ('foo_detail', (), {
'foo_id': str(self.id),
'name_slug': slugify(self.name),
})
Note that if I add a print slugify(self.name)
before returning, the slugified name is showing up in the console just fine.
When invoking {{ foo.get_absolute_url }}
in my templates, the result is always an empty string, and I don't get any errors.
I know I could replace the method with return '/foo/%s/%s' % (str(self.id), slugify(self.name))
, but I'm trying to get the permalink working so that my URL is only defined in one place. What am I doing wrong?
One thing to check for, because I also ran into this problem:
(?P<name_slug>\w+)
Is slugify adding hyphens anywhere? If so the regex won't match, hypens are a non-word character. To fix use [\w-]+
or similar.
这篇关于Django - 我如何获得永久性使用“一次性”金属块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!