将数据从数据库传递到表单作为属性

时间:2016-04-30 12:25:02

标签: django django-forms django-templates popover django-crispy-forms

我想渲染带有一些复选框的表单。这些复选框有一个带有附加信息的弹出框,它存储在数据库中。 如何在表单模板中访问其他数据?

我应该在视图中执行此操作:form = SomeForm(what goes here?)

它是否属于表单模板?

我在这里有点迷失,也许我只需要一个例子。

models.py:

class SomeModel(models.Model):
    name = models.CharField('Model name', unique=True, validators=[MinLengthValidator(1)], max_length=200)
    stuff = models.ForeignKey('Stuff',related_name='stuff')

    def __str__(self):
        return self.name

class Stuff(models.Model):
    name = models.CharField('Stuffname', unique=True, validators=[MinLengthValidator(1)], max_length=200)
    info = models.TextField('Info', validators=[MinLengthValidator(1)], max_length=300)

    def __str__(self):
        return self.name

forms.py:

class SomeForm(ModelForm):
    stuff = ModelChoiceField(queryset=Stuff.objects.all(), empty_label=None)
    def __init__(self, *args, **kwargs):
        super(SomeForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            'name',
            InlineCheckboxes('stuff'),
            Submit('save', 'save')
        )
    class Meta:
        model = Stuff
        fields = '__all__'

CustomForm.html:

<div class="controls {{ field_class }}"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap3/layout/field_errors_block.html' %}

{% for choice in field.field.choices %}

  {% if not inline_class %}<div class="checkbox">{% endif %}
    <label class="checkbox-img {% if inline_class %}checkbox-{{ inline_class }}{% endif %}
data-toggle="popover" data-trigger="hover" data-placement="top" data-title="{{choice.0}}" data-html="true" data-content="How to get the data in here? ">
        <input type="checkbox"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
    </label>
  {% if not inline_class %}</div>{% endif %}
{% endfor %}

{% include 'bootstrap3/layout/help_text.html' %}

如何解析数据内容属性中Stuff.info的值?

解决:

只需使用字段中的queryset属性。

CustomForm.html:

<div class="controls {{ field_class }}"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap3/layout/field_errors_block.html' %}

{% for choice in field.field.queryset%}

  {% if not inline_class %}<div class="checkbox">{% endif %}
    <label class="checkbox-img {% if inline_class %}checkbox-{{ inline_class }}{% endif %}
data-toggle="popover" data-trigger="hover" data-placement="top" data-title="{{choice.name}}" data-html="true" data-content={{ choice.info }}">
        <input type="checkbox"{% if choice.id in field.value or choice.id|stringformat:"s" in field.value or choice.id|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.id|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
    </label>
  {% if not inline_class %}</div>{% endif %}
{% endfor %}

{% include 'bootstrap3/layout/help_text.html' %}

0 个答案:

没有答案