samedi 6 juin 2015

Chained Dropdown in flask, get data from sqlite database

I am trying to get a html select tag with different counties (getting from database), when the user have selected a county, I want another select tag to enable and show the cities in that county (I have the data in a sqlite database, where the county id is in the city database). I am using python in Pycharm, with flask and I haven't found any good tutorials. Is there an easy way of using some extension to flask? I saw something about sijax, but I never understood how to use it. Can anyone help me understand this, it should be easy and is just annoying :P

The code I have is something like this, but I guess the city-part has to be created through some javascript thingy.

        <div class="form-group">
            <label for="inputCounty">County</label>
            <select class="form-control" id="select_county" name="select_county">
                <option value="">Choose county</option>
                {% for county in counties %}
                    <option value="{{ county.id }}">{{ county.name }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="form-group">
            <label for="inputCity">City</label>
            <select class="form-control" id="select_city" name="select_city">
                <option value="">Choose city</option>
                {% for city in cities %}
                    <option value="{{ city.id }}" class="{{ city.county }}">{{ city.name }}</option>
                {% endfor %}
            </select>
        </div>

I tried a "chained"-javascipt plugin but it didn't work, but thet's why I have class in the option tags.

Right now the counties and all cities are sent to the html template, but I don't think that will be sustainable later on because I want to add another dropdown with places in the cities as well, so then I have to send a lot of data that never will be used...

Sorry, I am new to this kind of programming but I want to learn some useful stuff :)

/Newbie

Aucun commentaire:

Enregistrer un commentaire