This article is mainly for the custom drop-down form production, the content of pull-down form is to take the database, that is, the dynamic implementation of the dropdown form
Body:
There are two ways to dynamically implement a drop-down form:
First, you manually write HTML template <form ...> </form>
In views, pass the required data to the past, such as Names_list = [1, 2, 3, 4]
In HTML
<select> {for in name_list%} <option value =" {{x }}">{{x}}</option> {% endfor%
Second, use form to automatically generate the form
This requires dynamic initialization of the form in views
Specific content to check the corresponding version of the Django document, keyword search model form
Here is the first method, for example:
HTML templates:
<select name= "Time" >
{%for downlist_tim in downlist_tim%}
<option value= "{{downlist_tim.finish_time}}" >{{downlist_tim.finish_time}}</option>
{%endfor%}
<option value= "Please select Time" selected= "selected" > Please select Time </option>
</select>
Views View function:
From django.shortcuts import renderfrom keywork.models Import lorderdownlist_tim = LOrder.objects.raw (' SELECT DISTINCT Id,finish_time from Keywork_lorder GROUP by finish_time ') #或者这里可以换成downlist_tim = LOrder.objects.values (' Finish_time ') . Distinct (), which uses the database for MySQL, has a tick to the next chapter on!return render (request, ' keywork/index.html ', {' Downlist_tim ':d Ownlist_tim,})
[Django] dropdown form and model query