django rest 프레임워크에 악리를 통해 데이터를 전송할 때 Bad Request 400을 가져옵니다.
django-rest-framework의 django가 API를 제공하고 Vuejs2가 프런트엔드로 사용되는 웹 어플리케이션을 만들려고 합니다.
여기 모델이 있습니다.화이
class ClientEntry(models.Model):
id = models.AutoField(primary_key=True)
parent_name = models.CharField(
max_length=30,
blank=False,
help_text="Enter parent's name"
)
student_name = models.CharField(
max_length=30,
blank=True,
help_text="Enter student's name"
)
tutor_name = models.CharField(
max_length=30,
blank=False,
help_text="Enter tutor's name"
)
mode_payment = models.CharField(
"mode of payment",
max_length=30,
blank=False,
help_text="how are we getting paid"
)
amount_recieved_parent = ArrayField(
models.CharField(
max_length=30
),
blank=True,
null=True
)
payment_mode_parent = ArrayField(
models.CharField(
max_length=30
),
blank=True,
null=True
)
date_payment_parent = ArrayField(
models.DateField(),
blank=True,
null=True
)
amount_payed_tutor = ArrayField(
models.CharField(
max_length=30
),
blank=True,
null=True
)
payment_mode_tutor = ArrayField(
models.CharField(
max_length=30
),
blank=True,
null=True
)
date_payment_tutor = ArrayField(
models.DateField(),
blank=True,
null=True
)
payment_status = models.CharField(
max_length=30,
blank=True,
null=True
)
tuition_status = models.CharField(
max_length=30,
blank=True,
null=True
)
payment_due_date = models.DateField(
blank=True,
null=True
)
class Meta:
ordering = ['id']
verbose_name_plural = "Client entries"
def __str__(self):
return str(self.parent_name)
serializers.py
class ClientEntrySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ClientEntry
fields = ('url', 'id', 'parent_name','student_name', 'tutor_name',
'mode_payment', 'amount_recieved_parent', 'payment_mode_parent',
'date_payment_parent', 'amount_payed_tutor', 'payment_mode_tutor',
'date_payment_tutor', 'payment_status', 'tuition_status',
'payment_due_date')
views.py
class ClientEntryViewSet(viewsets.ModelViewSet):
queryset = ClientEntry.objects.all()
serializer_class = ClientEntrySerializer
다음은 제 질문과 직접 관련된 3가지 vue 컴포넌트입니다.
Dashboard Detail.표시하다
export default {
name: 'DashboardDetail',
components: {
DashboardDetailEdit,
DashboardDetailPost
},
data () {
return {
entry: {},
paymentEntry: {
url: '',
amountRecievedParentArray: [],
paymentModeParentArray: [],
datePaymentParentArray: [],
amountPaidTutorArray: [],
paymentModeTutorArray: [],
datePaymentTutorArray: []
}
}
},
methods: {
getEntry() {
axios({
method: 'get',
url: 'http://127.0.0.1:8000/api/dashboard/' + this.$route.params.id + '/'
}).then(response => this.entry = response.data);
}
},
created () {
this.getEntry()
},
computed: {
paymentEntryComputed: function () {
if (this.entry !== null && this.entry.hasOwnProperty("amount_recieved_parent") && this.entry.amount_recieved_parent !== null)
this.paymentEntry.amountRecievedParentArray = this.entry.amount_recieved_parent
if (this.entry !== null && this.entry.hasOwnProperty("payment_mode_parent") && this.entry.payment_mode_parent !== null)
this.paymentEntry.paymentModeParentArray = this.entry.payment_mode_parent
if (this.entry !== null && this.entry.hasOwnProperty("date_payment_parent") && this.entry.date_payment_parent !== null)
this.paymentEntry.datePaymentParentArray = this.entry.date_payment_parent
if (this.entry !== null && this.entry.hasOwnProperty("amount_payed_tutor") && this.entry.amount_payed_tutor !== null)
this.paymentEntry.amountPaidTutorArray = this.entry.amount_payed_tutor
if (this.entry !== null && this.entry.hasOwnProperty("payment_mode_tutor") && this.entry.payment_mode_tutor !== null)
this.paymentEntry.paymentModeTutorArray = this.entry.payment_mode_tutor
if (this.entry !== null && this.entry.hasOwnProperty("date_payment_tutor") && this.entry.date_payment_tutor !== null)
this.paymentEntry.datePaymentTutorArray = this.entry.date_payment_tutor
if (this.entry !== null && this.entry.hasOwnProperty("url"))
this.paymentEntry.url = this.entry.url
return this.paymentEntry
}
}
}
</script>
Dashboard Detail Post.표시하다
<template>
<dashboard-detail-form @submit-query="addPaymentEntry"
:paymentEntry="clonePaymentEntry"
ref="dashDetailForm"></dashboard-detail-form>
</template>
<script>
export default {
components: {
DashboardDetailForm
},
name: 'DashboardDetailPost',
props: {
paymentEntry: Object
},
data(){
return {
clonePaymentEntry: {}
}
},
methods: {
addPaymentEntry (data) {
axios({
method: 'put',
url: this.paymentEntry.url,
data: {
amount_recieved_parent: data.amountRecievedParentArray,
payment_mode_parent: data.paymentModeParentArray,
date_payment_parent: data.datePaymentParentArray,
amount_payed_tutor: data.amountPaidTutorArray,
payment_mode_tutor: data.paymentModeTutorArray,
date_payment_tutor: data.datePaymentTutorArray
}
})
.then(() => {
this.$refs.dashDetailForm.resetForm()
})
.catch((error) => {
console.log(error)
})
}
},
created () {
this.clonePaymentEntry = this.paymentEntry
}
}
</script>
Dashboard Detail Form.표시하다
export default {
components: {
FormDropdown,
FormInput
},
name: 'DashboardDetailForm',
props: {
editDetailForm: Object,
paymentEntry: Object
},
data () {
return {
showForm: false,
form: {
amountRecievedParent: null,
paymentModeParent: '',
datePaymentParent: '',
amountPaidTutor: null,
paymentModeTutor: '',
datePaymentTutor: ''
},
paymentModeArray: [
{ value: "cash", text: "Cash" },
{ value: "paytm", text: "PayTM" },
{ value: "bank seth", text: "Bank Seth" },
{ value: "bank anuj", text: "Bank Anuj" },
{ value: "kotak", text: "Kotak" }
]
}
},
created () {
if (typeof this.editDetailForm !== "undefined") {
this.form.amountRecievedParent = this.editDetailForm.amountRecievedParent
this.form.paymentModeParent = this.editDetailForm.paymentModeParent
this.form.datePaymentParent = this.editDetailForm.datePaymentParent
this.form.amountPaidTutor = this.editDetailForm.amountPaidTutor
this.form.paymentModeTutor = this.editDetailForm.paymentModeTutor
this.form.datePaymentTutor = this.editDetailForm.datePaymentTutor
}
},
methods: {
formToggle () {
this.showForm = !this.showForm
},
resetDetailForm () {
this.form.amountRecievedParent = null,
this.form.paymentModeParent = '',
this.form.datePaymentParent = '',
this.form.amountPaidTutor = null,
this.form.paymentModeTutor = '',
this.form.datePaymentParent = ''
},
computePaymentEntry () {
this.paymentEntry.amountRecievedParentArray.push(this.form.amountRecievedParent)
this.paymentEntry.paymentModeParentArray.push(this.form.paymentModeParent)
this.paymentEntry.datePaymentParentArray.push(this.form.datePaymentParent)
this.paymentEntry.amountPaidTutorArray.push(this.form.amountPaidTutor)
this.paymentEntry.paymentModeTutorArray.push(this.form.paymentModeTutor)
this.paymentEntry.datePaymentTutorArray.push(this.form.datePaymentTutor)
this.validateBeforeSubmit()
},
validateBeforeSubmit () {
this.$validator.validateAll().then(() => {
if(!this.errors.any()) {
this.$emit('submit-query', this.paymentEntry)
this.formToggle()
}
})
}
}
}
</script>
이것으로 DashboardDetailForm 컴포넌트의 폼 속성 오브젝트가 올바르게 값을 취득하고 DashboardDetailPost에도 올바르게 전달됩니다.
그러나 PUT 요청의 Axios 블록을 포함하는 addPaymentEntry() 메서드를 실행하면 항상 django에서 Bad Request Error가 발생합니다.
Bad Request: /api/dashboard/1/
[05/Nov/2018 20:50:27] "PUT /api/dashboard/1/ HTTP/1.1" 400 414
처음에 mount_received_parent는 PositiveIntegerField였고 같은 에러가 발생하여 데이터베이스를 플래시한 후 CharField로 변경했지만 변경되지 않았습니다.이 에러를 해결할 수 있는 아이디어가 전혀 없습니다.
리노바는 댓글로 질문에 답했습니다."네트워크 탭에서 요청/응답 내용을 볼 수 있습니다."제 경우 Google Chrome 콘솔에서 400 Bad Request 오류가 발생했는데, 무엇이 잘못되었는지에 대한 자세한 정보를 어디서 얻을 수 있는지 알 수 없었습니다.
네트워크 탭에서 확인해보니 요청/응답에 대한 자세한 내용을 볼 수 있었고, 어떻게 해야 할지 알 수 있었습니다.
이것은 코멘트에 나타난 것처럼 질문의 작성자 @fluk3r에게도 효과가 있었습니다.
언급URL : https://stackoverflow.com/questions/53157277/getting-bad-request-400-when-sending-data-to-django-rest-framework-through-axios
'programing' 카테고리의 다른 글
"198.1994.Out Of Memory Error: Maven 빌드의 Perm Gen 공간" (0) | 2022.08.29 |
---|---|
스위치 문: 기본값은 마지막 대/소문자여야 합니다. (0) | 2022.08.27 |
패널 헤더 왼쪽에 아이콘이 있는 Vuetify 확장 패널 (0) | 2022.08.27 |
vue 이벤트와 vuex 돌연변이를 vue-devtools에서 제외할 수 있습니까? (0) | 2022.08.27 |
JDK 다이내믹프록시와 CGLib의 차이점은 무엇입니까? (0) | 2022.08.27 |