0
で2Dテーブル:塗りつぶしは-に私はDjangoのそれとテーブルを構築しようとしているバックリファレンス
- は、行と列の間の多対多の関係を使用します。
- 空白を使用できます。
- 拡張可能です。
- 行と列の名前で取得できる値を格納します。私はこれを行うために中間モデルを使用しようとしています(これが最善の方法であるかどうかはわかりません)。
値が参照されていません。私のHTMLテーブルはすべての可能な行と列をリストする必要があり、存在しないかもしれないので、テーブル値をループすることでこれを行うことはできません。ここで
は
models.py
from django.db import models
class Row(model.Models):
name = models.CharField(max_length=8)
class Col(model.Models):
rows = ManyToManyField(Row, through='Table')
name = models.CharField(max_length=8)
class Table(model.Models):
row = ForeignKey(Row)
col = ForeignKey(Col)
value = models.FloatField()
ビュー...私は何をしようとしているの例です:
views.py
from django.shortcuts import *
from test.app.models import *
def table(request):
rows = Row.objects.all()
cols = Col.objects.all()
values = Table.objects.all()
return render_to_response(
'app/table.html',
{'row_list': rows, 'col_list': cols, 'value_list': values}
)
HTMLコード(値にアクセスしようとして立ち往生):
table.html
{% extends "base.html" %}
{% block content %}
<h2>My Table</h2>
<ul>
<TABLE border=4 bgcolor=lightgray align=center>
<TR>
<TD align=center bgcolor=darkgray><strong></strong></TD>
{% for col in col_list %}
<TD align=center bgcolor=darkgray><strong>{{ col.name }}</strong></TD>
{% endfor %}
</TR>
{% for row in row_list %}
<TR>
<TD align=center bgcolor=darkgray><strong>{{ col.name }}</strong></TD>
{% for col in col_list %}
{% if row in col.rows.all %}
<!-- How can I access the table values here? -->
<TD align=left><input type="text" name="value" value="???" id="id_{{ col.id }}_{{ row.id }}" /></TD>
{% else %}
<TD align=left><input type="text" name="value" value="" id="{{col.id}}_{{row.id}}" /></TD>
{% endif %}
{% endfor %}
</TR>
{% endfor %}
</ul>
{% endblock %}
私は最終的に値を更新するフォームにしたいと思っていますが、今は自分のhtmlコードを動作させるだけです。どんな助けもありがとう。