Django使模型字段名称成为链接
我想要做的是在模型的字段名称上有一个链接。因此,当我使用管理界面填写表单时,我可以访问一些信息。
我知道这不起作用,但显示了我想要做的
class A(models.Model):
item_type = models.CharField(max_length=100, choices=ITEMTYPE_CHOICES, verbose_name="<a href='http://www.quackit.com/html/codes'>Item Type</a>")
其他选项是在该字段旁边添加描述。
我什至不知道从哪里开始。
what Im looking to do is to have a link on the name of a field of a model. So when im filling the form using the admin interface i can access some information.
I know this doesn't work but shows what i want to do
class A(models.Model):
item_type = models.CharField(max_length=100, choices=ITEMTYPE_CHOICES, verbose_name="<a href='http://www.quackit.com/html/codes'>Item Type</a>")
Other option would be to put a description next to the field.
Im not even sure where to start from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遗憾的是,这比乍一看要困难得多。从您定义
verbose_name
的时间点到它到达admin/includes/fieldset.html
{{ field.label_tag }} 的时间之间> 模板,该字符串会发生很多操作。本质上,无论您做什么,字符串都会被强制返回 unicode 并(最终)在label_tag
中转义。尝试使用mark_safe
或SafeUnicode
甚至|safe
模板过滤器都无法防止发生转义。这意味着您有三个选择:
在 django 表单内部进行大量的黑客攻击,以毫发无伤地携带 SafeUnicode 字符串。
在
admin/includes/fieldset.html
模板中手动构建字段的标签标记。请注意,该标签上有很多重要属性,例如 id、for、class 等。创建一个模板过滤器,用于解析标签标记内的字符串并将其转换为链接。
如果您擅长正则表达式,选项三实际上可能是最简单的。
Sadly, this is a lot harder than at first glance. Between the point at which you define
verbose_name
and the time it gets to{{ field.label_tag }}
in theadmin/includes/fieldset.html
template, a lot of manipulations happen to that string. Essentially no matter what you do, the string gets forced back into unicode and (ultimately) escaped inlabel_tag
. Trying to usemark_safe
orSafeUnicode
or even the|safe
template filters all fail to prevent the escaping that takes place.What that means is that you have three options:
Do a lot of hacking in the django form internals to carry a SafeUnicode string all the way through unharmed.
Manually construct the field's label tag in the
admin/includes/fieldset.html
template. Beware that there are a lot of important attributes that go on that label like id, for, class, etc.Create a template filter that parses the string inside the label tag and converts it into a link for you.
Option three there may actually be the simplest if you're any good at all with regexes.
这是真正应该在模板中处理的事情。
这是一种解决方法......
您可以创建一个名为“描述”的完整其他模型,然后将“项目类型”作为该表中的条目。
从那里,您可以查看永久链接装饰器 http ://docs.djangoproject.com/en/dev/ref/models/instances/#the-permalink-decorator 这将允许您创建任何给定“描述”对象的永久链接。
最后,查看 http://docs.djangoproject .com/en/1.1/ref/contrib/admin/#overriding-admin-templates 用于修改管理模板。在编辑
A 类
对象时,您可以将锚点插入该描述对象的永久链接。您想做的事情绝对是可能的。你只需要重新整理一下你的想法。
祝你好运!
This is something that should really be handled in the template.
Here's one way to go about it...
You can make a whole other model called "Description", then make "Item Type" as an entry in that table.
From there, you can check out the permalink decorator http://docs.djangoproject.com/en/dev/ref/models/instances/#the-permalink-decorator which will allow you to create permalinks to any given "Description" object.
Finally, look into http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#overriding-admin-templates for modifying admin templates. Where you are editing the
class A
objects, you can insert an anchor to the permalink of that Description object.What you are looking to do is definitely possible. You just need to reorganize your thinking.
Good luck!