作者s890269 (赛)
看板Python
标题[问题] 将表格输入的资料显示
时间Fri Sep 2 02:26:32 2016
我其实不太知道我的问题属於python还是html5
但我是参考python的书,所以想说发在这里
看我以下的内容可能有人已经知道是哪本书了
我想要做的功能是输入表格的值,并且将表格的值累计显示在上面
这是书籍上的范例
但{%for c in r.comment_set.all %}
这行我不太懂,专案中app的名称为restaurant
所以这个r指的是restaurant吗?
以及他的c in r.comment_set.all
因为用c in r.comment,所以之後的内容才可以是{{c.visitor}}?
还有他的set.all是甚麽意思?是指该评价功能上应该输入的都有输入完全?
还有他是用for 回圈来依序显现在上面的吗?
comment为我在views中define的东西,height也是
<table>
<tr>
<th>留言者</th>
<th>时间</th>
<th>评价</th>
</tr>
{% for c in r.comment_set.all %}
<tr>
<td> {{c.visitor}} </td>
<td> {{c.date_time1|date:"F j,Y" }} </td>
<td> {{c.content}} </td>
</tr>
{% endfor %}
</table>
<form atcion="" method="post">{% csrf_token %}
<table>
<tr>
<th><label for="id_visitor">留言者:</label></th>
<td> {{f.visitor}} </td>
<td> {{f.visitor.errors}} </td>
</tr>
<tr>
<td><label for="id_email">电子信箱:</label></td>
<td> {{f.email}} </td>
<td> {{f.email.errors}} </td>
</tr>
<tr>
<td><label for="id_content">评价:</label></td>
<td> {{f.content}} </td>
<td> {{f.content.errors}} </td>
</tr>
</table>
<input type="submit" value="给予评价">
</form>
希望的结果:
留言者 时间 评价
user 八月 19,2016 123456
s890269 九月 2,2016 我没意见~~
s890269 九月 2,2016 我没意见!!
----------------------------------------
以下为表格:
留言者:
电子信箱:
评价:
给予评价(按此按钮输入评价,然後会依序显示在上面)
这是我参考照打的
views:
def height(request):
if request.POST:
f1=HeightForm(request.POST)
if f1.is_valid():
cushion=f1.cleaned_data['cushion']
faucet=f1.cleaned_data['faucet']
date_time=timezone.localtime(timezone.now())
h = Height.objects.create(
cushion=cushion,
faucet=faucet,
date_time=date_time,
)
f1=HeightForm()
else:
f1=HeightForm()
return render_to_response('height.html',
RequestContext(request,locals()))
form:
class HeightForm(forms.Form):
cushion=forms.CharField(max_length=2)
faucet=forms.CharField(max_length=2)
models:
class Height(models.Model):
date_time=models.DateTimeField('date published')
cushion=models.CharField(max_length=255)
faucet=models.CharField(max_length=255)
def __unicode__ (self):
return self.name
<table>
<tr>
<th>时间</th>
<th>坐垫高度</th>
<th>龙头高度</th>
</tr>
{%for h in r.height_set.all %}
<tr>
<td> {{h.date_time|date:"F j,Y" }} </td>
<td> {{h.cushion}} </td>
<td> {{h.faucet}} </td>
</tr>
{% endfor %}
</table>
<form atcion="" method="post">{% csrf_token %}
<table>
<tr>
<th><label for="id_cushion">坐垫高度:</label></td>
<td>{{f1.cushion}}</td>
</tr>
<tr>
<th><label for="id_faucet">龙头高度:</label></td>
<td>{{f1.faucet}}</td>
</tr>
</table>
<input type="submit" value="纪录">
</form>
但我按了之後,却没有显示在上面
不太知道是有什麽问题,程式码应该是没问题
因为网页有成功跑出来
应该说,我想问的是
他能够让提交的讯息都显示在上面的关键是什麽?
我有漏打吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 218.164.162.219
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1472754395.A.4EB.html
1F:→ h5904098: 上面那个范例是comment有foreignkey关系才那样用吧 但 09/02 12:28
2F:→ h5904098: 你只有height一个model 直接for h in height 就可以显 09/02 12:28
3F:→ h5904098: 示了吧? 09/02 12:28
4F:→ s890269: 没有办法欸,一样只有页面刷新,没有值 09/02 14:02
5F:→ s890269: 会是因为要height_set.all? 09/02 14:22
6F:→ h5904098: 应该是你render的context里没有Height.objects.all() ? 09/02 16:57
7F:→ h5904098: 应该不是用_set.all 09/02 17:03
8F:→ h5904098: 你Height=Height.objects.all()有render进去 09/02 17:03
9F:→ h5904098: 直接for h in height 就有东西了吧? 09/02 17:04