Django:自定义特定表单字段的 HTML

发布于 2024-12-08 14:03:20 字数 1301 浏览 1 评论 0原文

 class ItemForm(forms.ModelForm):
     description = forms.CharField(label='Description', max_length=250, widget=forms.Textarea, required=False)
     image = forms.ImageField(label='Item Picture', max_length=50, required=False)
     start = forms.DateField(widget=SelectDateWidget, required=False)
     end = forms.DateField(widget=SelectDateWidget, required=False)
     cost_price = forms.CharField(label='Cost Price Per Unit', widget=???, max_length=5)

     class Meta:
         model = Item
         fields = ('image',
                   'name',
                   'description',
                   'quantity',
                   'start',
                   'end',
                   'cost_price',
                   'selling_price',
                   )

我需要在 cost_price 字段前面包含一个文本变量。

从文档中,我知道小部件类是我需要修改的,但我不太确定如何去做。

更新

因此,表单中的每个字段都由模板中的 {{ field }} 表示。此 {{ field }} 生成该特定字段的 HTML。我想修改 cost_price 字段的 HTML,以便可以将变量 {{currency_type }} 附加到 HTML 的前面。所以它应该看起来像这样:

<span>USD</span><input type="text" name="cost_price" id="id_cost_price">

现在我通过模板逻辑包含这个 {{currency_type }} 变量。我想知道是否可以通过自定义表单字段的 HTML 来做到这一点,因此出现了问题。希望这能更好地解释它!

 class ItemForm(forms.ModelForm):
     description = forms.CharField(label='Description', max_length=250, widget=forms.Textarea, required=False)
     image = forms.ImageField(label='Item Picture', max_length=50, required=False)
     start = forms.DateField(widget=SelectDateWidget, required=False)
     end = forms.DateField(widget=SelectDateWidget, required=False)
     cost_price = forms.CharField(label='Cost Price Per Unit', widget=???, max_length=5)

     class Meta:
         model = Item
         fields = ('image',
                   'name',
                   'description',
                   'quantity',
                   'start',
                   'end',
                   'cost_price',
                   'selling_price',
                   )

I need to include a text variable in front of the cost_price field.

From the docs, I know that the widget class is what I need to modify but I'm not too sure on how to go about doing it.

UPDATE

So each field in my form is represented by {{ field }} in my template. This {{ field }} generates the HTML for that particular field. I would like to modify the HTML of the cost_price field such that I can append a variable {{ currency_type }} to the front of the HTML. So it should look something like this:

<span>USD</span><input type="text" name="cost_price" id="id_cost_price">

Right now I am including this {{ currency_type }} variable through template logic. I was wondering if I could do this through customizing the form field's HTML hence the question. Hope this explains it better!

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

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

发布评论

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

评论(1

划一舟意中人 2024-12-15 14:03:20

您可以创建继承自 TextInput 小部件(用于 CharField)的自定义表单小部件并覆盖其渲染方法。这样您就可以准确地执行您想要的操作 - 在常规 TextInput 小部件 HTML 之前插入自定义 HTML。

from django.utils.safestring import mark_safe
from django.forms import widgets

# ...

# your custom widget class
class CostPriceWidget(widgets.TextInput):
    def render(self, name, value, attrs=None):
        return mark_safe(u'''<span>USD</span>%s''' % (super(CostPriceWidget, self).render(name, value, attrs)))

# your form class
class ItemForm(forms.ModelForm):
    # ...
    cost_price = forms.CharField(label='Cost Price Per Unit', widget=CostPriceWidget, max_length=5)
    # ...

希望这有帮助。

You can create custom form widget that inherits from the TextInput widget (which is used for CharField) and overrides it's render method. That way you can do exactly what you want to - insert custom HTML before regular TextInput widget HTML.

from django.utils.safestring import mark_safe
from django.forms import widgets

# ...

# your custom widget class
class CostPriceWidget(widgets.TextInput):
    def render(self, name, value, attrs=None):
        return mark_safe(u'''<span>USD</span>%s''' % (super(CostPriceWidget, self).render(name, value, attrs)))

# your form class
class ItemForm(forms.ModelForm):
    # ...
    cost_price = forms.CharField(label='Cost Price Per Unit', widget=CostPriceWidget, max_length=5)
    # ...

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文