6

我现在正在为一个要求而苦苦挣扎,我想在选择字段标签中添加一个图像,但我真的不知道该怎么做。我正在使用 django 表单向导来呈现表单。这是显示我想要实现的目标的图像: 在此处输入图像描述

这就是我现在所得到的(为了使单选按钮内联,我知道可以通过 css 实现): 在此处输入图像描述

这是forms.py:

from django import forms
from django.utils.translation import gettext as _


CHOICES=[('0','Pay by card'), ('1','Invoice')]




class PaymentForm(forms.Form):
    title = 'payment'
    payment_method = forms.ChoiceField(label = _("Payment Options"), choices=CHOICES, widget=forms.RadioSelect(), required = True)

我正在使用向导表单进行渲染:

 {{ wizard.form.payment_method.label_tag }}
                                {{ wizard.form.payment_method|safe }}
                                {{ wizard.form.payment.errors}}

除了自定义小部件之外,有人对此有任何建议吗?

4

4 回答 4

3

没有小部件:

from django.utils.safestring import mark_safe
CHOICES=[('0', mark_safe('Pay by card <img src="by_card.jpg"/>')),
         ('1', mark_safe('Invoice <img src="no_card.jpg"/>'))
]

信用:为 RadioSelect 中的每个选项设置 help_text

于 2014-01-23T01:23:11.360 回答
2

在您的模板中执行以下操作:

 {% for choice in wizard.form.payment_method.choices %}
    {{ choice.0 }} {# value #} {{ choice.1 }} {# value #}
    {% if choice.0 == PAYMENT_BY_PAYPAL %}
     ...
    {% endif %}
 {% endfor %}

你也可以写:

  {% for key, value in wizard.form.payment_method.choices %}
    {% if key == PAYMENT_BY_PAYPAL %}
     ...
    {% endif %}
 {% endfor %}
于 2013-07-19T13:25:14.230 回答
2

1)很快(但我不确定)调用一个“用卡支付”的函数并返回<img>...你需要的所有东西。

2)你可以像@Gahbu说的那样做

3)长[更好,我认为,但未经测试:(]:制作渲染器:

from myapp.my_widgets import CardsRadioFieldRenderer

CARD_CHOICE = '0'

CHOICES=[(CARD_CHOICE,'Pay by card'), ('1','Invoice')]

class PaymentForm(forms.Form):
    title = 'payment'
    payment_method = forms.ChoiceField(label = _("Payment Options"), choices=CHOICES,widget=forms.RadioSelect(renderer=CardsRadioFieldRenderer), required = True)

# myapp/my_widgets.py

class CardRadioInput(RadioInput):
    def __init__(self, name, value, attrs, choice, index):
        self.name, self.value = name, value
        self.attrs = attrs
        choice_value = force_text(choice[0])
        self.choice_value = choice_value
        if choice_value == CARD_CHOICE:
            choice_label = force_text(self.get_html_for_card_choice())
        else:
            choice_label = force_text(choice[1])
        self.choice_label = choice_label
        self.index = index

   def get_html_for_card_choice(self):
        #some logic to get the images tags (<img ...> <img ...>)
        return text


class CardsRadioFieldRenderer(RadioFieldRenderer):
    def __getitem__(self, idx):
        choice = self.choices[idx] # Let the IndexError propogate
        return CardRadioInput(self.name, self.value, self.attrs.copy(), choice, idx)
于 2013-07-19T13:40:31.003 回答
0

如果你想从模板中调整它,你需要使用告诉 Jinja 不要逃避它的“安全”。像下面

{{ some_object.some_property|safe }}

但是,如果您想在定义表单/模型表单时在 python 代码中使用它,只需使用 mark_safe,如下所示。

mark_up = 'this is a <a href="http://www.google.com">link</a>'
choices = list()
choices.append(('some_id', mark_safe(mark_up)))
self.fields['some_field'].choices = choices
于 2017-01-10T12:21:14.820 回答