动态添加的数据不使用 django 保存到数据库中

时间:2021-02-12 12:19:21

标签: mysql django database

enter image description hereenter image description here

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

1 个答案:

答案 0 :(得分:0)

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 %}
相关问题