2017-06-21 23 views
1

我想利用django中的默認幫助文本,但我不喜歡它的處理方式。我想要:以django格式全局更改幫助文本的行爲

<tr><label>djangolab</label><input>djangoinput</input><span>djangohelp></span><span class='onhovershowhelp'>?</span> 

默認情況下不提供最後一個元素。懸停在'?'上的CSS將改變幫助文本從隱藏到可見的可見性。

我希望事物能夠正常工作,因此'{{form}}'將以任何模型形式顯示。默認情況下,一些屬性(Z = 1,隱藏)

  • 添加另一個跨度中形成行

    1. 幫助文本跨度:所以我想在全球範圍。

    我不想爲每一個雛型/場等做到這一點,使用循環模板和手動構建該等等

  • +0

    你想一個幫助所有形式的文字都是一個字段? –

    +0

    @ArpitSolanki再次嗨!不,我想保留已由django提供的所有默認文本,完全按照原樣。我只是想改變一下渲染器。我想也許在一個泛型的ModelForm類中重寫'to_table',我所有的模型都繼承自我的模型,我開始着手這方面的工作 - 但是如果有人找到更好/更好的解決方案,我很樂意看到它。 – kabanus

    +0

    不完全確定,但你可以在你的表單中創建一個init函數,然後調用super,然後你可以更新一個像'self.fields ['myfield']這樣的屬性。widget.attrs.update({'class':'myfieldclass' })' –

    回答

    0

    明白了。有各種形式的繼承了這樣的事情(即_html_output呼叫直接從Django的源代碼所隱藏實現細節):

    import django.forms 
    
    class GenericForm(django.forms.ModelForm): 
        def as_table(self): 
         return self._html_output(
          normal_row='<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', 
          error_row='<tr><td colspan="2">%s</td></tr>', 
          row_ender='</td></tr>', 
          help_text_html='<a class="helptext">?<span>%s</span></a>', 
          errors_on_separate_row=False) 
         return html 
    

    和一些CSS陪這樣的:

    .helptext { 
        margin: 5px; 
        color: blue; 
    } 
    
    a.helptext span { 
        display:none; 
        width: 30em; 
        text-align: justify; 
        z-index:1; 
    } 
    
    a.helptext:hover span { 
        display:inline; 
        position:absolute; 
        background:#ffffff; 
        border:1px solid #cccccc; 
        color:#6c6c6c; 
    }