2017-06-21 47 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; 
    }