【问题标题】:Form input template does not save表单输入模板不保存
【发布时间】:2017-08-04 21:54:12
【问题描述】:

py 在一个 def 中,它在一个表格中列出了我的文章,其中包含我的模型表单的 inputtext、RequestEditForm、出现在每篇文章旁边的输入,以及一个用于保存已保存的 inpuText 数量的按钮。我的问题是该按钮什么都不做,因此它不会将输入的金额保存在数据库中。

    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)
       form = PedidoEditForm()
    if form.is_valid():
       form.save()
      pedido.estado = 'pendiente'
      pedido.fecha_pedido = datetime.now()
      pedido.save()

return render(request, 'admindata.html', locals(),{'form':form})

      <section id="contenido">
     <div class="container" style="margin:50px auto width="100%"">
    <form id="myform" method="post">
    <table id="example" class="table table-border table-striped table-hover">
        <thead>
            <tr> 
                <td>Servicio</td>
                <td>Cod experto</td>
                <td>Nombre</td>
                <td>stock</td>
                <td>Cantidad</td>
                <td>Fecha Pedido</td>
                <td>Estado</td>
                <td>Ingresar</td>
                <td></td>

            </tr>
        </thead>
        <tfoot>
            <tr>
                <td>Servicio</td>
                <td>Cod experto</td>
                <td>Nombre</td>
                <td>stock</td>
                <td>Cantidad</td>
                <td>Fecha Pedido</td>
                <td>Estado</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.articulo.stock }}</td>
                <td>{{ ped.cantidad }}</td>
                <td>{{ ped.fecha_pedido }}</td>
                <td>{{ ped.estado }}</td>
                <td>{% csrf_token %}
                    {{form.as_p}}</td>
                <td></td>
            </tr>
    {% endfor %}
    {% endif %}
       </tbody>
       </table>
       </form>

</div>
</section>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="{% static "js/bootstrap.min.js" %}"></script>
<script src="{% static "js/jquery.dataTables.min.js" %}"></script>
<script src="{% static "js/dataTables.bootstrap.min.js" %}"></script>
<script src="{% static "js/dataTables.checkboxes.min.js" %}"></script>
<script src="{% static "js/jquery.form.min.js" %}"></script>


<script>
 $(document).ready(function (){
 var table = $('#example').DataTable({

  'columnDefs': [
     {
        'targets': 0,
     }
  ],
  });

  table.column(7).every( function () {

  var column = this;
   var select = $('<input type="submit" form="myform" class= "btn btn-      success" value="Guardar">')
  .appendTo($(column.footer()).empty())
  .on('change', function() {
    var val = $.fn.dataTable.util.escapeRegex(
      $(this).val()
    );

    column
      .search(val ? '^' + val + '$' : '', true, false)
      .draw();
  });

column.data().unique().sort().each(function(d, j) {
  select.append('<option value="' + d + '">' + d + '</option>')
});

});

});

因为我缺少的按钮不起作用,所以我也有以下错误: 用户警告:

       A {% csrf_token %} was used in a template, but the context did not    provide the value.  This is usually caused by not using RequestContext.
     " A {% csrf_token %} was used in a template, but the context " help me pls!

【问题讨论】:

    标签: django python-2.7


    【解决方案1】:

    像这样使用render 而不是render_to_respoonse

    return render(request, template, locals())
    

    您在表单之外还有一个submit 按钮!

    您应该将其放在&lt;form&gt;&lt;/form&gt; 中或在&lt;input&gt; 中定义id of the form where it belongs。像这样:

    <input type="submit" form="myform" class= "btn btn-success" value="Guardar">
    

    【讨论】:

    • 我仍然无法让自己添加
    • 那意味着什么?
    • {% csrf_token%} {{Form.as_p}} Tr> 表尾的方式不一样?
    • 再次更新!
    • 感谢您的帮助先生,我不知道过去是什么,但现在我不认为按钮放在html中的任何地方:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多