gpt4 book ai didi

django - 如何将表格拆分为两列?

转载 作者:行者123 更新时间:2023-12-02 01:13:28 25 4
gpt4 key购买 nike

我有模型:

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>

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

最佳答案

我在 View 中使用它:

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>

而且有效。

关于django - 如何将表格拆分为两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14502221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com