如何发送表单数据,对象,图像?

时间:2018-10-14 19:19:32

标签: vue.js django-rest-framework

为什么我不能发送这两个表格值?在我的POST数据中,我有常规的表格数据,一张照片以及两个对象“ cat”和“ meddication” ...

我的帖子方法。...

  postCarelog(catID, catName) {
    axios.post(`/api/v1/carelogs/`, {
      cat: {id: catID, name: catName},
      weight_unit_measure: 'G',
      weight_before_food: this.weight_before_food,
      food_unit_measure: 'G',
      amount_of_food_taken: this.amount_of_food_taken,
      food_type: this.food_type,
      weight_after_food: this.weight_after_food,
      stimulated: this.stimulated,
      stimulation_type: this.stimulationType,
      medication: {name: this.medication,
        duration: '24',
        frequency: this.medication_dosage_given,
        dosage: this.dosage,
        notes: this.notes},
      medication_dosage_unit: 'ML',
      medication_dosage_given: this.medication_dosage_given,
      notes: this.notes,
      progress_photo: this.progress_photo.name,
    },{
      headers: {
        'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
      }
    })
      .then(response => {
        console.log(response);
        response.status === 201 ? this.showSwal('success-message','Carelog added') : null;
        this.getFeedings(catName);
      })
      .catch(error => {
        console.log(catID, catName);
        console.log(error);
        this.showSwal('auto-close', error);
      })
  },

我收到此错误...

  

{“ cat”:[“此字段为必填。”],“药物”:[“此字段为必填。”]}

我在后端具有DRF,并且在api / views.py ...(为简便起见,已对其进行编辑)中设置了MultiPartParser

...
from rest_framework.parsers import MultiPartParser

...
class CareLogViewSet(viewsets.ModelViewSet):
queryset = CareLog.objects.all()
serializer_class = CareLogSerializer
parser_classes = (MultiPartParser,)
filter_fields = ('cat__slug', 'cat__name', 'food_type')
lookup_field = 'slug'

那我为什么会收到错误消息?

更新:

根据要求,这是我的模板表格...

<template>
               <form id="carelogAddForm">
                          <b-collapse id="collapseForm" class="mt-2">
                            <fg-input label="Create New Carelog"
                                      class="column-sizing">
                              <div class="row">
                                <div class="col-md-2">
                                  <b-form-select v-model="food_type" :options="foodOptions"></b-form-select>
                                </div>
                                <div class="col-md-2">
                                  <b-input v-model="amount_of_food_taken" placeholder="Amount food taken"></b-input>
                                </div>
                                <div class="col-md-2">
                                  <b-form-input v-model="weight_before_food" placeholder="Weight Before Food"></b-form-input>
                                </div>
                                <div class="col-md-2">
                                  <b-input v-model="weight_after_food" placeholder="Weight After Food"></b-input>
                                </div>
                                <div class="col-md-2">
                                  <b-form-select v-model="stimulated" :options="stimulatedOps"></b-form-select>
                                </div>
                                <div class="col-md-2">
                                  <b-form-select v-model="stimulationType" :options="stimulationTypeOps"></b-form-select>
                                </div>
                              </div>
                              <div class="row">
                                <div class="col-sm-2">
                                  <b-form-select v-model="medication">
                                    <option selected :value="null">Medication</option>
                                    <option :value=catMed.name v-for="catMed in catMedications">{{catMed.name}}</option>
                                  </b-form-select>
                                </div>
                                <div class="col-sm-2">
                                  <b-input v-model="medication_dosage_given" placeholder="Med. Dosage"></b-input>
                                </div>
                                <div class="col-sm-4">
                                  <!--care photo upload-->
                                  <span v-if="window.width < 500">
                                    <input style="display: none" type="file" @change="onPhotoUpload" ref="fileInput2">
                                    <button id="progressImageBtn" class="btn" @click="$refs.fileInput2.click()">Upload Progress Image</button>
                                  </span>
                                  <span v-else>
                                    <input class="btn btn-primary" type="file" @change="onPhotoUpload">
                                  </span>
                                </div>
                              </div>
                            </fg-input>
                          </b-collapse>
                          <div class="row">
                            <div class="col-sm-12">
                            <button class="btn btn-sm btn-info float-right" @click='showButton = !showButton' v-if="showButton" v-b-toggle.collapseForm>Add New Log</button>
                            <button type="reset" class="btn btn-sm btn-warning float-right ml-2" @click='showButton = !showButton' v-if="!showButton" v-b-toggle.collapseForm>Cancel</button>
                            <button type="submit" class="btn btn-sm btn-success float-right" v-if="food_type !== 'MN' && !showButton"
                                      v-on:click="validateSubmitNoMom(scope.item.id, scope.item.name, scope.item.slug)" @click='showButton = !showButton'>Submit</button>
                            <button type="submit" class="btn btn-sm btn-success float-right" v-if="food_type === 'MN' && !showButton"
                                      v-on:click="validateSubmitMom(scope.item.id, scope.item.name)">Submit mom</button>
                            </div>
                          </div>
                        </form>
</template>

还有我的数据对象(警告很长,一般情况下,它们在应用程序中的运行情况更多)

data () {
  return {
    medication: null,
    modals: {
      basic: false,
      centered: false,
      custom: false,
      confirm: false
    },
    onFiltered: '',
    variableAtParent: 'DATA FROM PARENT!',
    activeName: 'first',
    currentSort:'name',
    currentSortDir:'asc',
    collapsed: true,
    medToEdit: [],
    feedToEdit: [],
    page: 1,
    CatIndex: 0,
    pagination: {
      perPage: 5,
      currentPage: 1,
      perPageOptions: [5, 10, 25, 50],
      total: 0
    },
    sortBy: null,
    sortDesc: false,
    sortDirection: 'asc',
    filter: null,
    searchQuery: '',
    propsToSearch: ['name', 'gender', 'age', 'id', 'birthday', 'catType'],
    cat: '',
    cat_type: '',
    example1: [],
    cats: [],
    thisCat: [],
    catFeedings: [],
    catMedications: [],
    careLogTableColumns: [],
    careLogColumns: [
      'id',
      'progressPhoto',
      'foodType',
      'amountOfFoodTaken',
      'stimulated',
      'weightBeforeFood',
      'weightAfterFood',
      'stimulationType',
      'medication',
      'medicationDosageGiven',
      'actions',
    ],
    tableColumns: [
      {
        key: 'id',
        prop: 'id',
        label: 'ID',
        minWidth: 90
      },
      {
        key: 'photo',
        prop: 'photo',
        label: 'Photo',
        minWidth: 100
      },
      {
        key: 'name',
        prop: 'name',
        label: 'Name',
        minWidth: 100,
        sortable: true
      },
      {
        key: 'gender',
        prop: 'gender',
        label: 'Sex',
        minWidth: 50,
        sortable: true
      },
      {
        key: 'birthday',
        prop: 'birthday',
        label: 'Birthdate',
        minWidth: 140,
        sortable: true
      },
      {
        key: 'age',
        prop: 'birthday',
        label: 'Age',
        minWidth: 100,
        sortable: false
      },
      {
        key: 'catType',
        prop: 'catType',
        label: 'Type',
        minWidth: 60,
        sortable: true
      },
      {
        key: 'actions',
        prop: 'actions',
        label: 'Actions',
        minWidth: 60
      },
    ],
    fuseSearch: null,
    weight_before_food: '',
    weight_after_food: '',
    amount_of_food_taken: '',
    food_type: null,
    foodOptions:[
      { value: null, text: 'Food Type' },
      { value: 'MN', text: 'Mom (Nursing)' },
      { value: 'BO', text: 'Bottle' },
      { value: 'BS', text: 'Bottle/Syringe' },
      { value: 'SG', text: 'Syringe Gruel'},
      { value: 'GG', text: 'Syringe Gruel / Gruel'},
      { value: 'G', text: 'Gruel'},
    ],
    notes:    '',
    stimulated: null,
    stimulatedOps: [
      { value: null, text: 'Stimulated' },
      'true',
      'false'
    ],
    stimulationType: null,
    stimulationTypeOps: [
      { value: null, text: 'Stim. Type' },
      { value: 'UR', text: 'Urine'},
      { value: 'FE', text: 'feces'},
      { value: 'UF', text: 'Urine / Feces'},
    ],
    showSuccess: false,
    showDanger: false,
    constant: 0,
    dismissSecs: 4,
    dismissCountDown: 0,
    dismissCountDown2: 0,
    nursing: false,
    handleAdd: true,
    showRow: true,
    showButton: true,
    showButton2: true,
    dosage_unit: 'ML',
    dosage:    '',
    dosageGuidelines: '',
    weightBeforeFood: '',
    amountOfFoodTaken: '',
    foodType: '',
    duration: '',
    frequency: '',
    name:   '',
    medication_dosage_given: '',
    progress_photo: null,
    window: {
      width: 0,
      height: 0
    }
  }
},

0 个答案:

没有答案