gpt4 book ai didi

python - 如何从下拉列表中选择多个值到views.py

转载 作者:行者123 更新时间:2023-12-04 10:39:25 25 4
gpt4 key购买 nike

我正在使用以下代码:

index.html

<div class="col-sm-3" style="margin-left: 30px;">
<select id="month" name="month" multiple>

<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>


</select>
</div>

Views.py
def internal(request):
try:
year = ''
month = []
year = request.GET.get('year')
month = request.GET.get('month')
print(month)
response_list = []
except Exception as e:
print(str(e))
return HttpResponse(json.dumps(response_list))

我在前端选择了多个值,但是当我在 views.py 中获取它时,只有一个选项被获取。如何获取从下拉列表中选择的所有值?

最佳答案

您需要使用 getlist(key) .

像这样:

month = request.GET.getlist('month')

get(key) 只会给你最后选择的值:

If the key has more than one value, it returns the last value.

关于python - 如何从下拉列表中选择多个值到views.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60001995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com