簡體   English   中英

django crispy-forms&floppyforms w / bootstrap:如何將help_text引入ModelForm?

[英]django crispy-forms & floppyforms w/ bootstrap: how to get help_text into a ModelForm?

我現在對django表格有一個小問題(但很煩人)。

我正在使用:

  • twitter bootstrap
  • django floppyforms(僅適用於html5小部件)
  • django crispy-forms(用於模板標簽和渲染)
  • 我的表格都是ModelForms ,如果可能的話不應該改變

我搜索了整個網絡並嘗試了很多東西,但我無法弄清楚我可以在代碼中注入help_text =“一些隨機幫助文本”的地方。 所以這是我的代碼(縮寫為出於理智原因):

#forms.py:

import floppyforms as forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import *
from crispy_forms.bootstrap import *
from courses.models import *

class CourseForm(forms.ModelForm):
    class Meta:
    model = Course
    widgets = {
        'title': forms.TextInput,  # This is a floppyforms widget
        ...
    }

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        self.helper.form_id = ''
        self.helper.form_class = 'form-horizontal'
        self.helper.form_action = ''  # redirect in the view
        self.helper.form_tag = True
        self.helper.help_text_inline = True  # means that I want <span> elements 
        self.helper.layout = Layout(
            Fieldset('Create a new course',  # fieldset label
                Field('title', placeholder="Something...", css_class="span4"),
                ...
            ),
            FormActions(
                Submit(
                    'submit',
                    'Submit',
                    css_class="btn-primary"
                )
            ),
        )
        super(CourseForm, self).__init__(*args, **kwargs)

我嘗試將它作為'attrs'字典注入到小部件中,並作為attr進入Field。

forms.TextInput(attrs={'help_text': 'Some help text'})
Field('title', help_text="Some help text", css_class="span4")

不用說,它沒有用。 我需要一個鈎子將幫助文本放入我的controls-div中的'span'或'p',而不是輸入小部件。

我的模板很小,如果可能的話應該保持這種狀態。 我不想迭代表單字段:

#create_course.html

{% extends 'base.html'%}
{% load crispy_forms_tags %}

{% block content%}
{% crispy form %}
{% endblock content%} 

這呈現如下html:

<div id="div_id_title" class="clearfix control-group">
  <label class="control-label requiredField" for="id_title">
    Title
    <span class="asteriskField">*</span>
  </label>
  <div class="controls">
    <input id="id_title" class="span4 textinput textInput" type="text" placeholder="Something..." maxlength="60" required="" name="title">
  </div>
</div>

使用幫助文本,它應如下所示:

<div id="div_id_title" class="clearfix control-group">
  <label class="control-label requiredField" for="id_title">
    Title
    <span class="asteriskField">*</span>
  </label>
  <div class="controls">
    <input id="id_title" class="span4 textinput textInput" type="text" placeholder="Something..." maxlength="60" required="" name="title">
    **<span class="help-inline">Supporting help text</span>**
  </div>
</div>

任何幫助表示贊賞! 我沒有嘗試通過views.py將代碼注入到表單中,但我認為沒有必要這樣做。 應該可以使用正確的鈎子和語法在forms.py中執行此操作。

很抱歉這個簡單問題的長文本;)

我發現了問題所在。 由於我沒有真正定義任何字段(我只定義字段小部件),我無法訪問help_text。 您可以在forms.py中定義Fields(這是不必要的和不好的做法,畢竟它們是ModelFields)或者在models.py中為模型設置help_text。

希望能幫助其他有類似問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM