Django - 管理自定义字段显示&行为

发布于 2024-11-29 08:47:45 字数 450 浏览 1 评论 0原文

让我们想象一下这个模型:

class ScribPart(models.Model):
  name = models.CharField(max_length=50, unique=True)
  text = models.TextField()

我想将特定的类附加到字段 text 并调用特定的 js 文件。这样管理页面就会像:

...
<script type="text/javascript" src="/static/js/mymarkup.js"></script>
...
<textarea id="id_text" name="text" class="mymarkup"></textarea>
...

如何使用小部件和/或自定义管理表单来做到这一点?

Let's imagine this model:

class ScribPart(models.Model):
  name = models.CharField(max_length=50, unique=True)
  text = models.TextField()

I'd like to attach a specific class to the field text and call a specific js file. So that the admin page would be like:

...
<script type="text/javascript" src="/static/js/mymarkup.js"></script>
...
<textarea id="id_text" name="text" class="mymarkup"></textarea>
...

How can I do that with a widget and/or custom admin form ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

跨年 2024-12-06 08:47:45

要在管理页面中插入

class ScribPartAdmin(model.ModelAdmin):
    ...
    your normal stuff...
    ...

    class Media:
        js = ('/path/to/your/file.js',)

ModelAdmin 媒体定义文档

现在将 class 属性添加到 textarea 我认为最简单的方法这样做是这样的:

from django import forms

class ScribPartAdmin(model.ModelAdmin):
    ...
    your normal stuff...
    ...

    class Meta:
        widgets = {'text': forms.Textarea(attrs={'class': 'mymarkup'})}

覆盖默认小部件文档

我应该补充一点,这种方法非常适合一次性使用。如果你想多次重用你的字段或JS,有更好的方法来做到这一点(字段的自定义小部件,如果JS与字段专门相关,则指定JS文件,扩展模板以在许多地方包含JS文件) 。

To insert a <script> in an admin page the simplest thing to do is:

class ScribPartAdmin(model.ModelAdmin):
    ...
    your normal stuff...
    ...

    class Media:
        js = ('/path/to/your/file.js',)

ModelAdmin media definitions documentation

Now to add the class attribute to the textarea I think the simplest way to do it is like this:

from django import forms

class ScribPartAdmin(model.ModelAdmin):
    ...
    your normal stuff...
    ...

    class Meta:
        widgets = {'text': forms.Textarea(attrs={'class': 'mymarkup'})}

Overriding the default widgets documentation

I should add that this approach is good for a one shot use. If you want to reuse your field or JS many times, there's better ways to do it (custom widget for the field, with JS file specified if the JS is exclusively related to the field, extending template to include a JS file at many places).

此岸叶落 2024-12-06 08:47:45

您必须创建一个模板,将其放入 templates/admin/change_form_scribpart.html 中并包含以下内容:

{% extends "admin/change_form.html" %}
{% load i18n %}

{% block content %}
    <script type="text/javascript" src="/static/js/mymarkup.js"></script>
    {{ block.super }}
{% endblock %}

另外,不要忘记在 ScribPart ModelAdmin 中激活这个新的管理模板:

class ScribPartAdmin(admin.ModelAdmin):   
    ordering = ...
    fieldsets = ...
    change_form_template = "admin/change_form_scribpart.html"

You have to create a template, put it in templates/admin/change_form_scribpart.html with this content:

{% extends "admin/change_form.html" %}
{% load i18n %}

{% block content %}
    <script type="text/javascript" src="/static/js/mymarkup.js"></script>
    {{ block.super }}
{% endblock %}

Also, don't forget to activate this new admin template in your ScribPart ModelAdmin:

class ScribPartAdmin(admin.ModelAdmin):   
    ordering = ...
    fieldsets = ...
    change_form_template = "admin/change_form_scribpart.html"
失与倦" 2024-12-06 08:47:45

您可以使用 json pack 发送表单并使用此代码获取(检查)

results = ScribPart.all()
    for r in results :

        if r.test == id_text:
            self.response.out.write("<script type='text/javascript' src='/static/js/"+r.name+"mymarkup.js'></script>")

You can send your form with json pack and get(check) with this code

results = ScribPart.all()
    for r in results :

        if r.test == id_text:
            self.response.out.write("<script type='text/javascript' src='/static/js/"+r.name+"mymarkup.js'></script>")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文