为什么我的表单没有显示在 example.html 中?
两个函数不能有相同的render_to_response
模板名(example.html)?
楷模:
class A(models.Model):
name = models.CharField(max_length=255)
quantity = models.IntegerField(default=1)
意见
def show(request):
a = A.objects.all()
if request.method == 'POST':
form = XForm(request.POST)
if form.is_valid():
f = form.cleaned_data['f']
return HttpResponseRedirect('/')
else:
form = XForm()
return render_to_response('example.html', {'a': a, 'form': form})
def show_f(request, a_id):
#how to access to data from form?
f = form.cleaned_data['f']
A.objects.filter(id = a_id).update(quantity = f)
return render_to_response('example.html')
示例.html:
{% for x in a %}
{{ x.name }}
<form action="/show_f/{{x.id}}/" method="post"/>
{{form}}
<input type="submit" value="Run"/>
</form>
{% endfor %}
编辑:
我想为数据库中的每个对象显示一个表单字段,并在输入数量并提交表单后运行 show_f 方法(更新数量)