2017-03-15 126 views
0

加载模板的表单模型时出现此错误,并在文本中将数字输入页面帮助 给出失败的原因: CSRF符号丢失或不正确。从Django,请帮助!禁止(403)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}} 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     &nbsp; 
     <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。 估计的问题是什么? 问候!

+0

我是否缺少一些东西或你的'表格'实际上从来没有完全呈现? '{else if'especialidad.estadistica ==“0”%}'在'' – 4140tm

+0

之前被调用是的先生,想法是如果统计量为0,它会将我发送到模型窗体,否则(否则)我列出表单中的表格。 –

+0

[Forbidden(403)CSRF验证失败。请求中止。即使使用{%csrf \ _token%}](http://stackoverflow.com/questions/20895526/forbidden-403-csrf-verification-failed-request-aborted-even-using-the-csr) – Nrzonline

回答

1

这很难调试,因为views.py的代码缩进已经搞乱了,但看起来您在那里有问题。在我的表单处理视图中,我通常会设置if测试来处理POST大小写,然后将GET的逻辑放在else分支中。清理视图应该有助于揭示问题(因为它看起来像POST案件中有两个案例,这对我来说没有意义)。我还建议你从render_to_response切换到render,并摆脱传递locals()的习惯,而不是在上下文中明确地传递你需要的东西。此外,看起来你已经搞乱了render_to_response的签名,因为你正在为你的上下文传递locals(),但是然后明确地传递了这个表单。我想你已经将你看过的两个不同的视图渲染混合起来了。我不是你想要做什么完全清楚,但我认为这种做法是清洁:

def ListAll(request, id_especialidad): 
    template = 'index2.html' 
    especialidad = Especialidad.objects.get(id=id_especialidad) 
    pedido = Pedido.objects.filter(especialidad=especialidad) 
    if request.method == 'POST': 
     form = PedidoEditForm(request.POST, instance=especialidad) 
     if form.is_valid(): 
      form.save() 
      # return a redirect here on success 
    # handles GET case and when form fails 
    user = request.user 
    if user.is_superuser: 
     template = 'admindata.html' 

    return render(request, template, {'form':form, 'pedido': pedido, 'especialidad': especialidad}) 
0

您放置ifelse和方式endif你将永远不会呈现一个完整的形式。我不确定这是你问题的原因,但肯定是一个问题。

尝试用这个例子:

<section id="contenido"> 
    <div class="container" style="margin:50px auto width="100%""> 
     {% if especialidad.estadistica == "0" %} 
     <form id="myform" method="POST"> 
      {% csrf_token %} 
      {{form.as_p}} 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      &nbsp; 
      <input type="submit" class= "btn btn-success" value="Guardar"> 
     </form> 
     {% 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> 
     {% endif %} 
    </div> 
</section> 
0

您可以通过第一个{%如果%}有一个破碎的形式。如果它是错误的,那么您没有打开表单标记的代码。

{% if especialidad.estadistica == "0" %} 
    <section id="contenido"> 
    <div class="container" style="margin:50px auto width="100%""> 
    <form id="myform" method="POST"><!-- IF FALSE, NEVER RENDERS --> 
    {% csrf_token %} 
     ... 
    {% else %}<!-- RENDER THE BEGGINING OF THE FORM AGAIN --> 
    <form id="myform" method="POST"> 
    {% csrf_token %} 
    </form> 
+0

而且其余的标签,你的第一个{%if%}不会正确地抨击html标签。 –

相关问题