Django:文本字段中的 DRY 内部链接
我有一个网站,其中有一堆“项目”,这些项目经常在其描述中相互引用(存储在文本字段中)。 我不想在描述中对项目之间的链接进行硬编码,而是希望通过使用某种标记来引用它们,例如在描述字段中:
Blabla text describing this project, this project was inspired by
{{ project "ProjectB"}} and lead to the development of {{ project "ProjectC" }}.
对其进行处理并转换为:
Blabla text describing this project, this project was inspired by
<a href="/projects/ProjectB">ProjectB</a> and lead to the development
of <a href="/projects/ProjectC">ProjectC</a>.
然后 清晰:描述是自由文本,可以在文本中的各个点以超链接的形式包含对其他项目的大量引用。在 CMS 中,这种效果通常是通过某种方式通过节点/对象 ID 链接到项目来实现的 - 这样,如果链接发生变化,仍然可以跟踪引用。
我考虑过:
- 将文本字段评估为模板并在描述中使用“url”模板标签。似乎是最简单的解决方案,但模板标签对于内容编辑器来说并不是特别友好,并且通过整个模板渲染器评估每个描述似乎有点麻烦。
- 实现一个模板标签,它只是重新实现一个基本的人造模板系统,以便仅出于此目的解析出一个漂亮的简单标签。
- 扩展 TextField 以在将描述保存到数据库之前对其进行预处理。
有人做过类似的事情吗?你有什么建议?
I have a site with a bunch of 'Projects' which often refer to each other in their descriptions (stored in a TextField).
Rather than hard-coding the links between projects in their descriptions, I'd like to keep things DRY by referring to them using some sort of token, for example, in the description field:
Blabla text describing this project, this project was inspired by
{{ project "ProjectB"}} and lead to the development of {{ project "ProjectC" }}.
Which is then processed and turned in to:
Blabla text describing this project, this project was inspired by
<a href="/projects/ProjectB">ProjectB</a> and lead to the development
of <a href="/projects/ProjectC">ProjectC</a>.
To be clear: the description is free text which can contain none to many references to other items as hyperlinks at various points in the text. In a CMS this effect is usually achieved through some way to link to items by node/object ID - so that if the link changes, the reference can still be followed.
I've considered:
- Evaluating the text field as a Template and using the 'url' templatetag in descriptions. Seems like the easiest solution but that templatetag isn't particularly friendly for content editors and evaluating each description through the entire Template renderer seems a bit cumbersome.
- Implementing a templatetag which just re-implements a basic faux-templating system to just parse out a nice simple tag just for this purpose.
- Extending the TextField to pre-process the description before it's saved to the database.
Has anyone done anything similar? What would you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚回答了一个类似的问题,看起来它可能会解决你的问题也是如此(如果三年后你仍然在寻找答案)。
我编写了一个模板过滤器来在显示之前解析文本字段中的自定义内部链接格式。我使用 Markdown 来解析我的文本字段,因此我使链接以 Markdown 格式返回,但它们可以轻松地编写为 HTML。
在此处查看我的答案。
更新:我发布了djangosnippets.org 上的修订版本,用于解析 markdown 格式链接内的内部链接,如以及他们自己。
I just answered a similar question on SO, and it seems like it might solve your problem as well (if by chance you're still looking for an answer three years later).
I wrote a template filter to parse the custom internal link format in the Textfield before display. I'm using Markdown to parse my textfields, so I made the links return in Markdown format, but they could easily be written as HTML instead.
Check out my answer here.
Update: I posted a revised version on djangosnippets.org that resolves internal links inside a markdown-formatted link, as well as on their own.
如果我遇到你的问题,你应该使用自定义模板处理器将字典传递给你的模板:
在settings.py中:
在myapp/myprocessor.py中:
或者如果你不想使用Django模板系统,你可以使用正则表达式(重新导入)
if I got your problem, you should use a custom template processor to pass a dictionary to your templates:
in settings.py:
in myapp/myprocessor.py:
or if you don't wanna use Django template system you can use regular expressions (import re)