0

我有一个项目,我试图重新组合一些字段,其中一些是可见的,而另一些将显示在一个表单中,这样我就可以在字段上应用 java 脚本来扩展。谁能告诉我如何在我的modelForm类和模板结构中做这样的事情?

模型.py

class Page(models.Model):
    # version control columns
    version_from = models.DateTimeField(auto_now_add=True, editable=False)
    version_to = models.DateTimeField(null=True, blank=True, editable=False)
    # data
    name = models.CharField(_('Name'), max_length=10, help_text=_('The name of the page will be used as a display name & in navigation menus'))
    show_in_navigation = models.BooleanField(default=True)
    slug = models.SlugField(_('Slug'), )
    subject = models.CharField(_('Subject'), max_length=30, )
    html_title = models.CharField(_('HTML Title'), max_length=15, blank=True, null=True)
    keywords = models.CharField(_('HTML Keywords'), max_length=200, blank=True, null=True)
    content = models.TextField(_('Content'), )
    parent = models.ForeignKey('self', null=True, blank=True)
    # audit
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, editable=False)

    # objects manager
    objects = PageManager()

    class Meta:
        unique_together = (
            'id',
            'slug'
        )

表格.py

class PageForm(forms.ModelForm):
    class Meta:
        model = Page

page-form.html

    <form class="form well" method="POST">
        {% csrf_token %}
            {% if form.errors %}
            <div class="alert alert-error">
                <a class="close" data-dismiss="alert" href="add_blog.html#">&times;</a>
                <h3>Error</h3>
                {{ form.non_field_errors }}
                {% for field in form %}
                        {{ field.errors }}
                {% endfor %}
            </div>
            {%endif%}

            {% csrf_token %}

            {# Include the hidden fields #}
            {% for hidden in form.hidden_fields %}
                {{ hidden }}
            {% endfor %}

            {# Include the visible fields #}
            {% for field in form.visible_fields %}

                    {{ field.label_tag }} {{ field }}
                    <span class="help-block">{{ field.help_text }}</span>

            {% endfor %}
        <br />
        <input type="submit" value='{% trans 'Login' %}' class="btn" />
    </form>

我的挑战是,如何使字段 slug 和关键字在 html 中呈现在 div 标签中并在最后呈现。

以下是我希望实现的示例

    <form class="form well" method="POST">
        {% csrf_token %}
            {% if form.errors %}
            <div class="alert alert-error">
                <a class="close" data-dismiss="alert" href="add_blog.html#">&times;</a>
                <h3>Error</h3>
                {{ form.non_field_errors }}
                {% for field in form %}
                        {{ field.errors }}
                {% endfor %}
            </div>
            {%endif%}

            {% csrf_token %}

            {# Include the hidden fields #}
            {% for hidden in form.hidden_fields %}
                {{ hidden }}
            {% endfor %}

            {# Include the visible fields #}
            {% for field in form.visible_fields %}

                    {{ field.label_tag }} {{ field }}
                    <span class="help-block">{{ field.help_text }}</span>

            {% endfor %}
            {% for collapsible_field in form.collapsible_fields %}
            <div class='collapsible'>
                    {{ field.label_tag }} {{ field }}
                    <span class="help-block">{{ field.help_text }}</span>                             
            </div>
            {%endfor%}
        <br />
        <input type="submit" value='{% trans 'Login' %}' class="btn" />
    </form>

上面的例子是为了进一步解释我的观点,但我会采用其他方法。请指教?

4

1 回答 1

2

您提出的所需解决方案看起来不错。只需将这些添加到您的 Form 类中:

class PageForm(forms.ModelForm):
    class Meta:
        model = Page

    def normal_fields(self):
        return [field for field in self if not field.is_hidden
               and field.name in ('field1', 'field2')]

    def collapsible_fields(self):
        return [field for field in self if not field.is_hidden
               and field.name in ('field4', 'field5', 'field3')]

然后在您的模板中使用以下方法:

{# Include the visible fields #}
{% for field in form.normal_fields %}

        {{ field.label_tag }} {{ field }}
        <span class="help-block">{{ field.help_text }}</span>

{% endfor %}
{% for field in form.collapsible_fields %}
<div class='collapsible'>
        {{ field.label_tag }} {{ field }}
        <span class="help-block">{{ field.help_text }}</span>                             
</div>
{%endfor%}
于 2012-07-29T13:47:17.033 回答