【发布时间】:2017-08-06 10:49:38
【问题描述】:
加载模板时出现这个错误表单的模型,并在文本发送我到页面帮助中输入金额 失败原因: CSRF 符号丢失或不正确。来自 Django,请帮助!
views.py:
def ListAll(request, id_especialidad):
especialidad = Especialidad.objects.get(id=id_especialidad)
if request.method == 'GET':
user = request.user
if user.is_superuser:
pedido = Pedido.objects.filter(especialidad=especialidad)
template = 'admindata.html'
return render_to_response(template,locals())
else:
if request.method == 'POST':
form = PedidoEditForm(instance=especialidad)
else:
form = PedidoEditForm(request.POST, instance=especialidad)
if form.is_valid():
form.save()
pedido = Pedido.objects.filter(especialidad=especialidad)
return render_to_response('index2.html',locals(), {'form':form})
模板html:
{% if especialidad.estadistica == "0" %}
<section id="contenido">
<div class="container" style="margin:50px auto width="100%"">
<form id="myform" method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" class= "btn btn-success" value="Guardar">
{% else %}
<table id="example" class="table table-border table-striped table-hover">
<thead>
<tr>
<td>Servicio</td>
<td>Cod experto</td>
<td>Nombre</td>
<td>Cantidad</td>
<td>Ingresar</td>
</tr>
</thead>
<tfoot>
<tr>
<td>Servicio</td>
<td>Cod experto</td>
<td>Nombre</td>
<td>Cantidad</td>
<td></td>
</tr>
</tfoot>
<tbody>
{% if pedido %}
{% for ped in pedido %}
<tr>
<td>{{ ped.especialidad.nombre }}</td>
<td>{{ ped.articulo.cod_experto }}</td>
<td>{{ ped.articulo.nombre }}</td>
<td>{{ ped.cantidad }}</td>
<td><a href="{% url "usuario:cant_ingresar" ped.id especialidad.id %}" method='GET' type="submit" class="btn btn-primary pull-right" value="editar" onclick="document.location.reload();"/>Ingresar</a></td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</form>
</div>
</section>
</div>
{% endif %}
模型形式:
from django import forms
from django.forms import ModelForm
from .models import Pedido, Especialidad
class PedidoEditForm(forms.ModelForm):
cantidad = forms.IntegerField(label='Cantidad:', widget=forms.TextInput(attrs={'size':'10'}))
class Meta:
model = Pedido
fields = [
'cantidad',
]
class EstadisticaForm(forms.ModelForm):
estadistica = forms.IntegerField(label='Estadistica Menusal:', widget=forms.TextInput(attrs={'placeholder':'Ingrese numero pacientes'}))
class Meta:
model = Especialidad
fields = [
'estadistica',
]
在此使用第二种:EstadisticaForm。 估计问题是什么? 问候!
【问题讨论】:
-
我是否遗漏了什么,或者您的
form实际上从未完整呈现?{% if especialidad.estadistica == "0" %}的else在</form>之前被调用 -
是的,先生,我们的想法是,如果统计数据为 0,它会将我发送到模型表格,否则(否则)我会列出表格中的表格。