Python Django中的提及/内部链接
我有很多模特。所有这些模型都有一个方法Python Django中的提及/内部链接,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,我有很多模特。所有这些模型都有一个方法get\u absolute\u url和一个字段text。我想在文本字段中创建内部链接,就像维基百科一样 维基百科页面中的内部链接仅指其他页面。我需要链接到我所有的模型 我可以为内部链接创建一个模式,并用一个硬编码的url替换这个模式,但这并不是一个好主意,因为链接可以更改。因此,如果我可以参考get\u absolute\u url,那就最好了 另一种选择是使用模板标记将特定模式更改为链接 应该怎么做?是否有任何开源项目已经完成了这项工作?几天前,我想回
get\u absolute\u url
和一个字段text
。我想在文本
字段中创建内部链接,就像维基百科一样
维基百科页面中的内部链接仅指其他页面。我需要链接到我所有的模型
我可以为内部链接创建一个模式,并用一个硬编码的url替换这个模式,但这并不是一个好主意,因为链接可以更改。因此,如果我可以参考get\u absolute\u url
,那就最好了
另一种选择是使用模板标记将特定模式更改为链接
应该怎么做?是否有任何开源项目已经完成了这项工作?几天前,我想回答同样的问题,我使用了一个模板过滤器。我的链接是相对的URL,不是绝对的,但是你可以很容易地调整它,你也可以调整正则表达式模式来匹配你喜欢的链接标记 使用过滤器,链接只在显示时才被查找,因此如果视图的URL已更改,则应使用
reverse()
查找自动更新
我还使用标记来处理我的描述字段,因此我使链接返回标记格式的链接,而不是HTML,但您也可以对此进行调整。如果使用Markdown,则首先要放置此筛选器
因此,要显示带有内部链接的描述文本字段,模板中的内容如下:
from django import template
from django.core.urlresolvers import reverse
from my.views import *
register = template.Library()
@register.filter
def internal_links(value):
"""
Takes a markdown textfield, and filters
for internal links in the format:
{{film:alien-1979}}
...where "film" is the designation for a link type (model),
and "alien-1979" is the slug for a given object
NOTE: Process BEFORE markdown, as it will resolve
to a markdown-formatted linked name:
[Alien](http://opticalpodcast.com/cinedex/film/alien-1979/)
:param value:
:return:
"""
try:
import re
pattern = '{{\S+:\S+}}'
p = re.compile(pattern)
#replace the captured pattern(s) with the markdown link
return p.sub(localurl, value)
except:
# If the link lookup fails, just display the original text
return value
def localurl(match):
string = match.group()
# Strip off the {{ and }}
string = string[2:-2]
# Separate the link type and the slug
link_type, link_slug = string.split(":")
link_view = ''
# figure out what view we need to display
# for the link type
if(link_type == 'film'):
link_view = 'film_detail'
elif(link_type == 'person'):
link_view = 'person_detail'
else:
raise Exception("Unknown link type.")
link_url = reverse(link_view, args=(link_slug,))
entity = get_object_or_404(Entity, slug=link_slug)
markdown_link = "[" + entity.name + "](" + link_url + ")"
return markdown_link
{{entity.description}内部链接}标记}
(有关写入和注册筛选器的详细信息,请参阅。)
至于具体的过滤器本身,我是这样做的:
from django import template
from django.core.urlresolvers import reverse
from my.views import *
register = template.Library()
@register.filter
def internal_links(value):
"""
Takes a markdown textfield, and filters
for internal links in the format:
{{film:alien-1979}}
...where "film" is the designation for a link type (model),
and "alien-1979" is the slug for a given object
NOTE: Process BEFORE markdown, as it will resolve
to a markdown-formatted linked name:
[Alien](http://opticalpodcast.com/cinedex/film/alien-1979/)
:param value:
:return:
"""
try:
import re
pattern = '{{\S+:\S+}}'
p = re.compile(pattern)
#replace the captured pattern(s) with the markdown link
return p.sub(localurl, value)
except:
# If the link lookup fails, just display the original text
return value
def localurl(match):
string = match.group()
# Strip off the {{ and }}
string = string[2:-2]
# Separate the link type and the slug
link_type, link_slug = string.split(":")
link_view = ''
# figure out what view we need to display
# for the link type
if(link_type == 'film'):
link_view = 'film_detail'
elif(link_type == 'person'):
link_view = 'person_detail'
else:
raise Exception("Unknown link type.")
link_url = reverse(link_view, args=(link_slug,))
entity = get_object_or_404(Entity, slug=link_slug)
markdown_link = "[" + entity.name + "](" + link_url + ")"
return markdown_link
你能为你的目的给出一个用例吗?我真的不明白你为什么想要twitter、instagram、facebook和维基百科这样的网站。将用户指向其他页面,查看页面上是否有其他地方提到某个页面。为什么要把事情弄得如此复杂?只要使用命名的URL,您就可以了!但我希望用户链接到其他页面/用户。所以我不能使用命名的URL。我还发布了一个解析markdown格式链接中的内部链接以及它们自己的链接的方法。