django继续在表格中填充表单数据

时间:2019-10-10 03:30:29

标签: django django-forms

我对Django很陌生,我需要继续将表单数据添加到同一页面的html表中。我可以填充一次,但是每次都需要继续添加。

请参阅以下gif:

sample data

我需要添加第二次添加的数据以填充第二行的表中。 请帮助我

Form.py

from django import forms

class vm_provisioning_form(forms.Form):
    name = forms.CharField()
    email = forms.EmailField(

views.py

from django.shortcuts import render
from django.http import HttpResponse
from Forms import forms

def vm_provisioning(request):

     form_vm_provisoning_info = forms.vm_provisioning_form()
     hidden_values = []
     if request.method == 'POST':
            values = []
            form_vm_provisoning_info = forms.vm_provisioning_form(request.POST)
            if form_vm_provisoning_info.is_valid():
            # DO SOMETHING CODE
                print("VALIDATION SUCCESS!")
                name = form_vm_provisoning_info.cleaned_data['name']
                email = form_vm_provisoning_info.cleaned_data['email']

                values.append([name, email])

                return render(request, 'Forms_template/vm_provisioning/vm_provisioning_form.html', {'forms': form_vm_provisoning_info,'data': values })

     return render(request, 'Forms_template/vm_provisioning/vm_provisioning_form.html', {'forms': form_vm_provisoning_info})

form_vm_provisoning_info.html

<!-- templates/vm_provisoning/form_vm_provisoning_info.html -->
<!DOCTYPE html>

{% extends 'Forms_template/base.html' %}

{% block title %}Login in registration{% endblock %}


{% block content %}

 <h1> Vm provisoning form</h1>

 <div class="container">
 <h1>Please Fill details here!</h1>
 <form method="POST">
   {{ forms.as_p  }}
  {% csrf_token %}

  <section>
    <!-- hidden_data= forms.CharField(widget=forms.HiddenInput(), required=False) -->
  {% if data %}


      <table border="1">
        <tr>
            <th>Name</th>
            <th>email</th>
        </tr>
        <!-- <p>{{ data }}</p> -->
        {% for mapdata in data %}
<!-- <p>{{ mapdata }}</p> -->
        <tr>
                <td>{{ mapdata.0 }} </td>
                <td>{{ mapdata.1 }}</td>
        </tr>
        {% endfor %}
      </table>
  {% endif %}

</section>

   <input type="submit" class='btn btn-primary' value="Submit">
 </form>
</div>




{% endblock %}

1 个答案:

答案 0 :(得分:0)

看看您的观点。这可能是因为每次发送POST请求时您都在分配新的values列表,而不是尝试在上面给出它,还必须将values列表也发送到GET方法,因此它始终显示数据。

def vm_provisioning(request):

     form_vm_provisoning_info = forms.vm_provisioning_form()
     hidden_values = []
     values = []
     if request.method == 'POST':

            form_vm_provisoning_info = forms.vm_provisioning_form(request.POST)
            if form_vm_provisoning_info.is_valid():
            # DO SOMETHING CODE
                print("VALIDATION SUCCESS!")
                name = form_vm_provisoning_info.cleaned_data['name']
                email = form_vm_provisoning_info.cleaned_data['email']

                values.append([name, email])

                return render(request, 'Forms_template/vm_provisioning/vm_provisioning_form.html', {'forms': form_vm_provisoning_info,'data': values })

     return render(request, 'Forms_template/vm_provisioning/vm_provisioning_form.html', {'forms': form_vm_provisoning_info,'data': values})