Search code examples
pythondjangodjango-viewsdjango-formsfile-handling

how to read an excel file on django using pandas?


I am working on a website that takes your city and area and the food that you want to eat as input. Then, it shows you a list of all the restaurants in your area that are offering the food that you desire. My website gets the list of all the restaurants (from admin) through an Excel file. The problem that I'm facing right now is that I'm unable to read the Excel file through pandas.

My views.py file is as follows:

class Addcity(View):

    def post(self, request, *args, **kwargs):  
        
        file_name= request.POST.get('FileName')
       
        #some extra lines of code
 
        df = pd.read_excel(file_name,names=["name","rating","distance"])

        print(df)
       
        return render(request, 'appmanager/addcity.html')

Currently, whenever I upload a file, it displays an error(no such file exists), I even tried to keep my file in the same directory as my views.py file, but still didn't work.

My addcity.html file is as follows:

{% extends 'appmanager/base.html' %}

{% block content %}

<div class="container mb-5">
    
    <div class="row justify-content-center mt-1">
        <div class="card col-md-5 col-sm-12  p-4 text-center">
            <form method="POST" action = "{% url 'welcome' %}" >
                {% csrf_token %}
                 <div class="form-group">
                 

                  <label for="FileName" class="form-label">Upload a file</label>
                  <input class="form-control" type="file" name="FileName" />
               
               
                  
                </div>
                <button type="submit" class="btn btn-primary mb-2">Enter</button>

              </form>
              

        </div>

    </div>
</div>
{% endblock content %}

Anyone here who could possibly help me with my issue, any help, no matter how small, would be greatly appreciated. Regards.


Solution

  • Building on the answer given above, you can do this by creating a django form. Follow the following steps:

    Create a forms.py and create a form. use the following code as a reference:

    from django import forms
    
    class Mynewform(forms.Form):
    
        Myname= forms.CharField(label='Enter your name')
        Myage= forms.CharField(label='Enter your Age')
        Form_file = forms.FileField(label='choose a file')
    
    

    You can read the Excel file by adding the following code in your Post request.

    from forms import Mynewform
    
    form = Mynewform(request.POST,request.FILES)
    if form.is_valid():
        name = form.cleaned_data['Myname']
        age = form.cleaned_data['Myage']
    df = pd.read_excel(request.FILES['Form_file'], names=[Your Column names])
    

    Lastly, add the following snippet to your HTML file:

            <form action="{% url 'viewfile_url' %}" method="post" enctype="multipart/form-data">
              {% csrf_token %}
              <table>
                {{ form.as_table }}
              </table>
              <input type="submit" value="Submit">
            </form>