H
H
huko2015-09-07 09:57:04
JavaScript
huko, 2015-09-07 09:57:04

How to dynamically add new select menus after selecting the previous one?

Django has a database with track names.
Based on this base, you need to create a playlist through the select menu, the number of tracks can be very different, so after selecting a track, you need to automatically show a new similar menu.
How to do it better?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IvanOne, 2015-09-07
@huko

The task is not fully described, but based on what I have, I can assume that there is a playlist model and tracks cling to it through many to many, in which case I would apply formsets. But if you need it to be asynchronous, then just send ajax on the server when saving the track, save the model, and generate the form in the template. Read about formsets in the docs where it is well described.
If you need to load something in the selects additionally, then use jquery, let's say there are categories in the form, choosing categories, load tracks, the easiest way is jquery, hang an event on the select

$("select").change(function(){
})

And in this function you send a request to the server where you specify the category, on the server in the view you select the tracks you need, it makes sense to select only id and names. using json, pack a string into json and send a response, and then process the request on the client and generate an option.

D
Dave, 2015-09-07
@djay

1. Django has no base. It is in MySQL there is a base. And in the database there is a table with the names of the tracks. Django pings the database and pushes the result into a dictionary.
2. Dynamic element loading is usually implemented either manually via Jquery + Ajax, or by data binding Angular + its http service.
Googled it myself on the 2nd point, I'm too lazy to paint everything in detail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question