【问题标题】:Dynamically added data not saving into database using django使用 django 动态添加的数据不保存到数据库中
【发布时间】:2021-05-16 04:45:43
【问题描述】:

enter image description hereenter image description here

动态增值不保存到数据库中。该值仅保存我们第一次给出的值。并且额外添加的数据没有保存到数据库中。

【问题讨论】:

  • 我认为您应该提供更多代码供其他人阅读。检查stackoverflow.com/help/how-to-ask
  • 请帮助我完成这项工作。提前谢谢..
  • 是的,如果他们知道导致问题的原因,我和其他人会帮助您(您应该提供所有链接或与您正在处理的问题相关的代码。这就是为什么我让您问基于此链接stackoverflow.com/help/how-to-ask 的一个好问题。
  • 是的,我提供了

标签: mysql django database


【解决方案1】:
Models.py:

class ordernewmodel(models.Model):   
    process_name = models.CharField(max_length=100)
    machine = models.CharField(max_length=100)
    add_info = models.CharField(max_length=100, unique=True)


forms.py:

class ordernewform(forms.ModelForm):
    class Meta:
        model = ordernewmodel
        fields = "__all__"




Views.py:


def ordernewview(request):
    if request.method == 'POST':
       
        if request.POST.get('process_name ') and  request.POST.get('machine') and request.POST.get('add_info')) :
            ordsave = ordernewmodel()          
            ordsave.process_name= request.POST.get('process_name')
            ordsave.machine= request.POST.get('machine')
            ordsave.add_info= request.POST.get('add_info')
            cursor = connection.cursor()
            cursor.execute("call orderordernew('"ordsave.process_name"' +'"ordsave.machine"' +'"ordsave.qty"' )")
            return render(request, 'my-order.html')
        else:
            print("nothing has to be print")
            return render(request, 'ordernew.html')
   
my-order.html:


<div class="col-md-12">
    <div class="card">
        
        <div class="card-body">
            <form id="myform" method="POST" action="/ordernewview/">
                {% csrf_token %}
                
                <div id="second">
                    <div class="row">
                        
                        <div class="col-md-4">
                            <label class="bmd-label-floating"></label>
                            <input type="text" class="form-control" name="process_name" id="process_name" placeholder="process_name" />
                            {{ form.process_name }}
                        </div>
                        <div class="col-md-4">
                            <label class="bmd-label-floating"></label>
                            <input type="text" class="form-control" name="machine" id="machine" placeholder="machine" />
                            {{ form.machine }}
                        </div>                    

                        <div class="col-md-4">
                            <label class="bmd-label-floating"></label>
                            <input type="text" class="form-control" name="add_info" id="add_info"
                                placeholder="add_info" />
                            {{ form.add_info }}
                        </div>
                       
                        <input type="button" class="btn btn-primary pull-right" name="vendor" &nbsp;
                            value="add vendor" />
                        <input onclick="addRow(this.form)" type="button" class="btn btn-primary pull-right" ;" &nbsp;
                            value="add process" />

                    </div>
                </div>               
                <div class="clearfix"></div>
            </form>
        </div>
    </div>
</div>

<script>
    var rowNum = 0;
    function addRow(frm) {
        rowNum++;
        var row = '<label id="rowNum' + rowNum + '"><div class="col-md-3"><label class="bmd-label-floating"></label><input type="text" class="form-control" name="process_name" id="process_name" placeholder="process_name" />< input type = "text" class="form-control" name = "machine[]" value = "' + frm.machine.value + '"  id = "machine" placeholder = "machine" /><input type="text" class="form-control" name="add_info[]" value="' + frm.add_info.value + '" id="add_info" placeholder="add_info" /></div><input type="button" value="Remove" onclick="removeRow(' + rowNum + ');"></label>';
        jQuery('#second').append(row);
        frm.process_name.value = '';
        frm.machine.value = '';   
        frm.add_info.value = '';
    }
    function removeRow(rnum) {
        jQuery('#rowNum' + rnum).remove();
    }
</script>
{% endblock content %}


<!-- Specific Page JS goes HERE  -->
{% block javascripts %}{% endblock javascripts %}

【讨论】:

  • 请帮我做这件事。
  • 没人帮我
猜你喜欢
  • 2017-08-31
  • 1970-01-01
  • 2016-03-23
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多