4

I am trying to generate two dropdown lists in django using a pandas dataframe. I am declaring the dataframe in the views.py file of the django project (no idea if it is the best way to do it) as I want to exploit all the functions of pandas such as unique() and all the possible selection I can do with the dataframe before sending the information to the webpage.

...views.py
def index(request):
    dataframe=pd.read_excel("myFile.xlsx")
    countries = dataframe['Country'].unique()
    cities = dataframe['City'].unique()
    context = {'countries' : countries, 'cities' : cities}
    return render(request, 'myApp/index.html', context}

while the index.html is

.../myApp/index.html
<select>
  {% for country in countries %}
  <option value="{{ country }}">{{ country }}</option>
  {% endfor %}
</select>
<select>
  {% for city in cities%}
  <option value="{{ city }}">{{ city }}</option>
  {% endfor %}
</select>

Now, I would like to select a 'Country' from its dropdown list and have only the cities belonging to that country in the second dropdown.

Is it possible (and how can I do it)?

Having in mind that the final work should be a dashboard (having also React.js and D3.js/NVD3.js so that a selection on dropdowns, search bars or plots will affect the whole webpage) is it a good approach to have a pandas dataframe defined in the views.py or is there any other way to do it? (Actually I would like to use unique() and pandas in the index.html, but I haven't found a way at the moment) Data will be available from an excel/csv file.

simon90
  • 123
  • 1
  • 8

0 Answers0