简体   繁体   中英

How to make a query from a selected choice from the user from a drop-down list in Django

I am new here. In an internship context, I have to developp a website in which the user will have the opportunity to select a program, a localisation or a thematic he wants to visualise in a heatmap.

To do so, I decided to use Django. I am encoutering 2 issues:

  • First one: I have a mysql database constituted with 1 table with the location names (detailed in many columns) and the coordinates, and one table by program of the raw datas. I need to find a way to join the two tables but one localisation name can have different coordinates depending on the program. So i need to concatenate 2 columns by table (2 columns from the raw datas that I join to two columns from the table with the coordinates)

For now I have thought about using new_var = table.objects.annotate but i cannot join the two newly variables... Do you have any ideas?

  • Secondly: The user is supposed to choose a localisation from a drop-down list, that i can use to filter my database and display the map as he wishes.

For now I have that:

(views.py)

''' def map(request):

m = folium.Map(location=[-17.4889,-149.90017], zoom_start=11, tiles='CartoDB Dark_Matter', control_scale=(True))

if request.method == 'GET':

    featured_filter = request.GET.get('site_name')

    if request.GET.get('nom_site'):

        data_list = table.objects.filter(localisation = featured_filter).values_list('latitude', 'longitude', 'proportion')

else :

    data_list = table.objects.all().values_list('latitude', 'longitude', 'proportion')

plugins.HeatMap(data_list).add_to(m)

m = m._repr_html_()

context = {

    'm': m,

    "showname": name}

return render(request, 'map.html', context)

'''

(map.html)

Select a site

{% for nom in showname %} {{name.localisation}} {% endfor %}

My issue is that "featured_filter" is an empty QuerySet. So can I get the selected value from the html interface into my python code?

Thank you for your help !

I do not understand very well what you want exactly, but I'll give you a series of tips to see if they can help you:

1 - If what you need is to join two database tables, you can use a many-to-many relationships ( Documentation )

2 - It is possible that this problem is better to face it with a form and the POST method:

  1. The user selects the option
  2. Send the form with the POST method
  3. Run the function that performs the filtering
  4. The function returns the result through the context and re-rendering the HTML.

3 - If you do not want to use forms, you can create a Custom Template Tags and JavaScript. This method is more complicated so I do not recommend it. ( Custom template tag ) ( InnerHTML Javascript )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM