2016-05-10 28 views
1

我想在Django中創建一個類似的窗體,就像您可以在提醒的重複節中的Google日曆中找到的一樣。 enter image description here如何將radiogroup select與Django形式的純輸入相結合

class ReminderFlatForm(ModelForm): 
    WEEKS = (
     (1, 'M'), 
     (2, 'T'), 
     (3, 'W'), 
     (4, 'T'), 
     (5, 'F'), 
     (6, 'S'), 
     (7, 'S'), 
     ) 
    REPEATS_BY = (
     (8, 'day of the month'), 
     (9, 'day of the week') 
     ) 
    ENDS = (
     (1, 'Never'), 
     (2, 'After <input type="text" id="after_id"> occuriencies'), 
     (3, '<input type="text" id="ondate_id">') 
     ) 

    weeks = forms.MultipleChoiceField(
     choices=WEEKS, 
     label="", 
     required=False, 
     widget=forms.CheckboxSelectMultiple 
     ) 
    months = forms.MultipleChoiceField(
     choices=REPEATS_BY, 
     label="", 
     required=False, 
     widget=forms.RadioSelect 
     ) 
    ends = forms.MultipleChoiceField(
     choices=ENDS, 
     label="Ends", 
     required=True, 
     widget=forms.RadioSelect 
     ) 

HTML元素但是相反,我有純文本。 我怎樣才能用Django的表單來做到這一點?

此外,如果重要,我使用脆皮形式。

回答

2

你需要他們使用的元素mark_safe與HTML

ENDS = (
     (1, 'Never'), 
     (2, mark_safe('After <input type="text" id="after_id"> occuriencies')), 
     (3, mark_safe('<input type="text" id="ondate_id">')) 
     ) 
+1

許多THX,只是看到了網絡督察,並沒有逃過 – Denis

0

此外,你可以循環在模板中的字段和油漆他們任何你想要的。種:

{% for field in form %} 
    <div class="fieldWrapper"> 
     {{ field.errors }} 
     {{ field.label_tag }} {{ field }} 
     {% if field.help_text %} 
     <p class="help">{{ field.help_text|safe }}</p> 
     {% endif %} 
    </div> 
{% endfor %} 

所以,你會定義域爲純文本(在你的形式),然後爲它們畫在你的模板之間你的HTML元素。

https://docs.djangoproject.com/es/1.9/topics/forms/

+0

我可以再補充純HTML。但我需要一個可重用的表單。 – Denis

+0

您的表單可以重複使用。你正在定義他們的領域,這將包含他們的價值觀。您決定如何繪製它的方式必須另一方面在模板中製作。 –

相關問題