django.utils.datastructures.MultiValueDictKeyError REST API发布请求

时间:2018-05-19 08:06:39

标签: python-3.x post request

在发布请求时获取django.utils.datastructures.MultiValueDictKeyError:'email_id'。这是我的代码

views.py:

def post(self, request):
    video_file = self.request.data['media_file']
    user_email_id = self.request.data['email_id']

    file_storage = FileSystemStorage()
    saved_file_name = file_storage.save(str(uuid.uuid4()), video_file)

upload.py:

headers = {"Authorizarion": "auth/key"}
data = {"email_id": "email@email.com",
        "media_file": open(video.file_path, "rb")}
response = requests.post("/api/url", headers=headers, data=data)

当尝试通过Postman工作上传文件时,因为它处理内容类型,但尝试在后端上传时很难清除它。

1 个答案:

答案 0 :(得分:2)

您似乎未通过<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" tools:context=".DashboardActivity" tools:showIn="@layout/activity_dashboard"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/colorPrimary" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="150dp" android:text="Match Starts in 02:24:39" android:textSize="20dp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2"> <android.support.v7.widget.CardView android:id="@+id/liveMarket" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="10dp" app:cardBackgroundColor="@color/white" app:cardCornerRadius="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="5dp" android:src="@drawable/badge_critical_charts" /> <TextView android:id="@+id/live_Market" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="12dp" android:text="Live Market" android:textColor="#000" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/card2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="5dp" app:cardBackgroundColor="@color/white" app:cardCornerRadius="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="5dp" android:src="@drawable/bookkeeping" /> <TextView android:id="@+id/MyBook" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="12dp" android:text="My Book" android:textColor="#000" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:orientation="horizontal" android:weightSum="2"> <android.support.v7.widget.CardView android:id="@+id/card3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="10dp" app:cardBackgroundColor="@color/white" app:cardCornerRadius="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="5dp" android:src="@drawable/charicon_statistics" /> <TextView android:id="@+id/win_loss_stats" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="12dp" android:text="Win|Loss Stats" android:textColor="#000" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/card4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="5dp" app:cardBackgroundColor="@color/white" app:cardCornerRadius="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="5dp" android:src="@drawable/calendar_png_picture" /> <TextView android:id="@+id/Schedule" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="12dp" android:text="Schedule" android:textColor="#000" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:orientation="horizontal" android:weightSum="2"> <android.support.v7.widget.CardView android:id="@+id/card5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="10dp" app:cardBackgroundColor="@color/white" app:cardCornerRadius="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="5dp" android:src="@drawable/pie_chart" /> <TextView android:id="@+id/Poll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="12dp" android:text="Poll" android:textColor="#000" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/card6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:elevation="5dp" app:cardBackgroundColor="@color/white" app:cardCornerRadius="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="5dp" android:src="@drawable/results_icon_png_2" /> <TextView android:id="@+id/Recent_Result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="12dp" android:text="Recent Result" android:textColor="#000" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> </LinearLayout> </LinearLayout> - 您可以在发布之前在email_id打印其值吗?

为安全起见,请在upload.py中提供默认值:

views.py

如果字段丢失,请将video_file = self.request.data.get("media_file", None) user_email_id = self.request.data.get("email_id", None) 替换为您想要的任何内容。

更新:当然,您会遇到以您的方式上传文件内容的问题。要上传实际文件,您应在None来电中使用files结构,例如:

requests.post()

然后在Django方面,您必须将内容检索为:

headers = {"Authorizarion": "auth/key"}
data = {"email_id": "email@email.com"}
files = {"media_file": open(video.file_path, "rb")}
response = requests.post("/api/url", headers=headers, data=data, files=files)