如何将表格拆分为两列?

时间:2013-01-24 13:14:48

标签: django django-forms django-templates

我有模特:

class Post(models.Model):
    path = 'images' + str(datetime.now().year) + '/' + str(datetime.now().month)
    image = models.ImageField(upload_to=path, null=True)
    recommended = models.BooleanField(default = False)
    promoted = models.BooleanField(default = False)
    title = models.TextField(blank = True)
    intro = RichTextField(config_name='full_ck', blank = True)
    text = RichTextField(config_name='full_ck', blank = True)

,表格:

class Form(forms.ModelForm):
    id = forms.ModelChoiceField(queryset=Post.objects.all(), widget=forms.HiddenInput())

    class Meta:
        model = Post

和模板:

<table cellpadding="0" cellspacing="0">
<formset>
{% for field in form %}
    {% if field.is_hidden %}
        {{ field }}
    {% else %}
        <div class="fieldWrapper">
             {% if field.errors %}<div class="errorbox">{% endif %}
                <p>{{ field.label_tag }}</p>
                <p>{{ field }}{% block formextrafields %}{% endblock %}</p>
                <p></p>
            {% if field.errors %}<p>{{ field.errors }}</p></div>{% endif %}
        </div>
    {% endif %}
{% endfor %}
</formset>
</table>

但我想将表格分成两列。首先可以是intro,text和title字段,也可以是第二个。怎么做?

2 个答案:

答案 0 :(得分:3)

我在视图中使用它:

form = list(form)

,在模型I中设置顺序:

class Meta:
    model = Post
    fields = (my fields in order)

并在模板中:

<!-- first -->
<table cellpadding="0" cellspacing="0">
<formset>
{% for field in form|slice:":3" %}
    [...]
{% endfor %}
</formset>
</table>

<!-- second -->
<table cellpadding="0" cellspacing="0">
<formset>
{% for field in form|slice:"3:" %}
    [...]
{% endfor %}
</formset>
</table>

它有效。

答案 1 :(得分:1)

您可以使用crispy表单进行布局并添加css类:https://github.com/maraujop/django-crispy-forms