我使用的是Django的formset這種模式:在Django自定義表單集模板
class Book(models.Model):
book_id=models.AutoField(primary_key=True,unique=True)
book_name=models.CharField(max_length=30)
publisher_name=models.CharField(max_length=40)
author=models.ForeignKey(Author)
該formset被這樣定義:
BookFormset = inlineformset_factory(Author, Book,
fields=('book_id','book_name', 'publisher_name'), extra=1,
can_delete=False)
模板是:
{{ formset.non_form_errors.as_ul }}
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle row1,row2 %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
的字段按列顯示,但我希望它們按行顯示。
以上代碼生成的輸出是這樣的:
Book name Publisher name
book_field Publisher_field
我想輸出看起來像這樣:
Book name book_field
Publisher name Publisher_field
我怎樣才能做到這一點?
雅你是對的,字段名是大膽的風格,如何改變把一個規則在樣式表正常的 – user2086641 2013-04-06 09:59:35
('字體重量:normal')。或者用'td'代替'th'。 – 2013-04-06 10:08:07