2

我有模型:

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>

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

4

2 回答 2

5

我在视图中使用它:

form = list(form)

,在模型中我设置顺序:

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>

它有效。

于 2013-01-24T14:23:15.730 回答
1

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

于 2013-01-24T13:17:45.610 回答