' WSGIRequest'对象没有属性' FILE'

时间:2017-08-23 12:33:37

标签: python django excel file

我想创建一个带有excel文件并阅读其内容的应用程序,而不使用表单和模型。

我一直在标题中收到错误,我想知道错误是什么。

这是我的HTML:

<div style="width: 800px; margin: 0 auto;">
    <form enctype="multipart/form-data"  action="." method='POST'> {% csrf_token %}
        <input type="file" name="excelfile">
        <input type="submit" value="Submit" />
    </form>
</div> 

这是我的观点:

def uploadexcelfile(request):
    if request.method == "POST":
        excel = request.FILE['excelfile'].read()

        print('Opening workbook...')

        wb = openpyxl.load_workbook(excel)
        activesheet = wb.active
        print(activesheet.title)

        sheet = wb.get_sheet_by_name(activesheet.title)

        print('Reading rows...')
        for row in range(1, sheet.max_row + 1):
            url = sheet['A' + str(row)].value

            print(url)
    else:
        return render(request, 'uploadexcelfile.html')

1 个答案:

答案 0 :(得分:2)

我认为您正在寻找request.FILES而不是<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="0" android:layout_marginTop="10dp" android:textSize="20dp" android:textColor="#000" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="What is" android:layout_marginTop="30dp" android:textSize="20dp" android:gravity="center"/> <LinearLayout android:weightSum="2" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_weight="1.5" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter Percentage" android:layout_marginTop="30dp" android:textSize="20dp" android:maxLength="3" android:inputType="number" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="%" android:layout_marginTop="30dp" android:textSize="20dp" android:layout_weight="0.5" android:gravity="center"/> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="of" android:layout_marginTop="50dp" android:textSize="20dp" android:gravity="center"/> <EditText android:layout_weight="1.5" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter Number" android:layout_marginTop="30dp" android:textSize="20dp" android:inputType="number" android:gravity="center"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Calculate" android:layout_alignParentBottom="true"/>