简体   繁体   English

使用 Vue 和 Django 发布动态表单数据

[英]Post Dynamic form data using Vue and Django

I am using Vue.js to post data from a regular.html form to Django. Been using python and Django for a few days now.我正在使用 Vue.js 将数据从 regular.html 表单发布到 Django。几天来一直在使用 python 和 Django。

There is a section of my form which contains an area that allows the user to dynamically add form fields(regular inputs and file uploads).我的表单有一部分包含一个区域,允许用户动态添加表单字段(常规输入和文件上传)。 I increment name value by using the index everytime the user adds another row.每次用户添加另一行时,我都会使用索引来增加名称值。 I am not sure how to go about storing these form data.我不确定 go 如何存储这些表单数据。

在此处输入图像描述


HTML HTML

....
<table class="table" id="documentsTable">
    <thead>
        <tr>
           <th>Type</th>
           <th>Number</th>
           <th>File</th>
           <th>Expiry Date</th>
           <th></th>
        </tr>
   </thead>
   <tbody>
        <tr v-for="(required_document, doc_index) in required_documents" :key="doc_index">
           <td>
               <select :name="`type${doc_index}`" class="form-control" :ref="`type${doc_index}`">
                    <option :value="index" v-for="(document_type, index) in document_types">[[document_type]]</option>
               </select>
           </td>
           <td>
               <input type="text" :name="`number${doc_index}`" class="form-control" :ref="`number${doc_index}`">
           </td>
           <td>
               <input type="file" :name="`file${doc_index}`" class="form-control" :ref="`file${doc_index}`">
           </td>
           <td>
               <input type="date" :name="`date_expiry${doc_index}`" class="form-control" :ref="`date_expiry${doc_index}`">
           </td>
           <td>
               <button class="btn btn-danger delete-row" @click.prevent="removeRow(doc_index)"><i class="pe-7s-trash"></i></button>
           </td>
         </tr>
   </tbody>
</table>

....Vue Section
...
save() {
    form = document.getElementById('individualForm');
    var formData = new FormData(form);

    axios.post('/customers/individual/store', formData).then((response) => {

    }).catch((errors) => {
        console.log(errors)
    });
}

views.py视图.py

def store_individual(request):
    if request.method == 'POST':

        individual_form = IndividualForm(request.POST)

        // for dynamic section
        required_document_form = RequiredDocumentForm(request.POST, request.FILES)

forms.py forms.py

class RequiredDocumentForm(forms.ModelForm):
class Meta:
    model = RequiredDocument
    fields = ['type', 'number', 'file', 'date_expiry']

TYPES = (('', 'Select Type'), ('passport', 'Passport'), ('national_id', 'National ID'),
         ('drivers_licence', "Driver's Licence"),
         ('nis', "NIS Number"))

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)


    total_documents = int(args[0]['total_documents'])

    for i in range(total_documents):
        # type0, #type1, number0, number1, etc
        type_field_name = 'type%s' % (i,)
        number_field_name = 'number%s' % (i,)
        file_field_name = 'file%s' % (i,)
        date_expiry_field_name = 'date_expiry%s' % (i,)

        self.fields[type_field_name] = forms.CharField(required=False, widget=forms.Select(choices=self.TYPES, attrs={'class': 'form-control', }))
        self.fields[number_field_name] = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control', }))
        self.fields[file_field_name] = forms.FileField(max_length=32,
                       widget=forms.ClearableFileInput(attrs={'multiple': False, 'class': 'form-control', }))
        self.fields[date_expiry_field_name] = forms.DateField(required=False, widget=SelectDateWidget(attrs={'class': 'form-control', }))

The Output Output

  • The output of the "print(request.POST)" is outputted in a QueryDict “print(request.POST)”的output在一个QueryDict中输出
  • The output of the "print(request.FILES)" is outputted in a MultiValueDict “print(request.FILES)”的output在MultiValueDict中输出

在此处输入图像描述

This is where I'm stuck.这就是我被困的地方。 I don't how to store the information after this.在此之后我不知道如何存储信息。

Call the form save function after validation.验证后调用表单保存 function。

if required_document_form.is_valid():
    required_document_form.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM