2013-07-19 43 views
5

我現在正在努力滿足一項要求,我想添加一個圖像到選擇字段標籤,我真的沒有線索如何做到這一點。我正在使用django表單嚮導來呈現表單。 這裏是圖像這說明我想達到的目標: enter image description here在django中添加自定義html到choicefield標籤

這裏就是我有現在(使單選按鈕內嵌,我知道它可以通過CSS來實現): enter image description here

這裏是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}} 

除了自定義小部件之外,任何人都有任何建議嗎?

+1

取決於您如何渲染表單。如果使用form.as_p()或類似的函數構建,則必須覆蓋此行爲。你也可以在html中手動完成你的表單。 https://docs.djangoproject.com/zh/dev/topics/forms/ – Jingo

+0

我剛剛編輯我的問題。 – Maverick

回答

1

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) 
+0

呃,即使我不確定是否可以實現第一點,但是Gahbu的解決方案現在不適合我,並且我保留第3點作爲最後一個選項,我只是不確定是否有任何簡單的解決方案可以實現這,所以想到這裏發佈。 – Maverick

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 %} 
+0

剛編輯我的問題,我使用表單嚮導進行渲染。 – Maverick

+0

是的,我也是,請看上面。 – gpichot

+0

是PAYMENT_BY_PAYPAL,鍵名?我的問題是'0'? – Maverick

2

沒有一個小部件:

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"/>')) 
] 

信用:setting help_text for each choice in a RadioSelect

0

如果你想twe的從模板中獲取它,你需要使用「安全」,告訴Jinja不要逃避它。像下面

{{ some_object.some_property|safe }} 

然而,如果你想在定義表格/的ModelForm只需使用mark_safe如下使用它你的Python代碼中。

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