如何将表格拆分为两列?

Nip*_*ips 2 django django-templates django-forms

我有型号:

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)
Run Code Online (Sandbox Code Playgroud)

, 形式:

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

    class Meta:
        model = Post
Run Code Online (Sandbox Code Playgroud)

和模板:

<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>
Run Code Online (Sandbox Code Playgroud)

但我想将表格分成两栏。第一个可能是简介、文本和标题字段,第二个可能是其他字段。怎么做?

Nip*_*ips 5

我用这个来查看:

form = list(form)
Run Code Online (Sandbox Code Playgroud)

,在模型中我设置顺序:

class Meta:
    model = Post
    fields = (my fields in order)
Run Code Online (Sandbox Code Playgroud)

并在模板中:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

它有效。