【发布时间】:2022-01-22 09:23:28
【问题描述】:
嘿,我看到了很多关于这个的问题,但我不知道为什么它不适合我。我的home.html 文件中有这段代码:
<form action="/result/" method="post">
<div class="form-locat-att">
{% csrf_token %}
Location: <input type="text" name="location_input" required/>
<p></p><br>
<button class="btn btn-block btn-round", name="search_button">Search</button>
<p></p>
<h5>Zabytki</h5> <p></p>
<input type="checkbox" id="museum" name="att[]" value=" Museum OR">
<label for="muzeum"> Museum</label>
<input type="checkbox" id="statue" name="att[]" value=" Statue OR">
<label for="pomnik"> Statue</label>
<input type="checkbox" id="castle" name="att[]" value=" Castle OR">
<label for="zamek"> Castle</label>
<input type="checkbox" id="palace" name="att[]" value=" Palace OR">
<label for="palac"> Palace</label> <p></p>
</div>
</form>
这是我的views.py:
def result(request):
if request.method == 'POST' and 'search_button' in request.POST:
attrac = request.POST.getlist('att[]')
locat = request.POST.get('location_input')
print(attrac)
# (here i have some irrelevant code where i render my context)
return render(request, 'przewodnik_app/result.html', context)
return render(request, 'przewodnik_app/result.html')
我正在尝试打印attrac,它应该为我提供我选中的复选框中的值。例如,当我使用带有 id 的 .get 方法时
request.POST.get('博物馆')
它返回正确的值。当我使用name 时,列表始终为空。
【问题讨论】: