[Django] dropdown form and model query

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.