Invalid file path or buffer object type: <class 'NoneType'> python-django

Viewed 682

I have a issue on read excel file uploaded by user,here my code

 if request.method == 'GET':
        add_file = request.FILES.get('monthly_file')
        df = pd.read_excel(add_file)

When i give request.Files['monthly_file] it gave multidict key value error,. So im using request.FILES.get('monthly_file') to get user uploaded file. But it gives Invalid file path or buffer object type: <class 'NoneType'> value error,. How can i resolve it.. Thanks in advance,..

1 Answers

Please make sure you have used the POST method in forms and similarly you recieve the file on the POST method of your views...

Template.html

<form method="POST">
    {% csrf_token %}
    <!-- OTHER FORM FIELDS -->
    <input type="submit" VALUE = "SUBMIT">
</form>

VIEWS.PY

if request.method == 'POST':
    try:
        add_file = request.FILES.get('monthly_file')
        df = pd.read_excel(add_file)
    except:
        print('NO FILE FOUND')
Related