我是Django的新手,因此如果我犯了愚蠢的错误,我深表歉意。
这是我到目前为止的代码:
对于views.py:
def bylog(request):
if request.POST.get('Filter'):
return render(request, 'index.html', context)
filtered_login = Invalid.objects.values_list('login').distinct()
filtered = []
for item in filtered_login:
filtered.append(item[0])
results = {'results': results, 'filtered': filtered}
return render(request, 'bylog.html', context)
以下是bylog.html的代码段:
<select id>"dropdown">
{% for item in filtered %}
<option value={{ item }}">{{ item }}</option>
{% endfor %}
</select>
<input type="submit" value="Filter" name="Filter" />
我的主要目标是从下拉列表中获取值,然后在用户单击Filter
按钮后,该值将传递到另一个模板。
那有可能吗?
感谢您的帮助。
我想实现的目标的基本目标是POST
在Django中进行管理,这意味着您希望将模板中的所有数据/变量发送到视图,然后对其执行任何操作(将其发送到另一个模板,或存储...)。
基本的是(使用HTML表单,而不是Django表单):
- Create a HTML form in the template
- Add the selects/inputs with the data you want to manage and a button/input to make the post
- Manage the post in the view
例子
模板形式
<form id="" method="post" action=".">
{% csrf_token %}
<select id="any_name" name="any_name">"dropdown">
{% for item in filtered %}
<option value={{ item }}">{{ item }}</option>
{% endfor %}
</select>
<input type="submit" value="Filter" name="Filter" />
</form>
view.py
def your_view(request):
if request.method == 'POST': # If anyone clicks filter, you receive POST method
data = request.POST['any_name']
# Do what you need here with the data
# You can call another template and send this data
# You can change any_name for the variable you want, changing the name and id in the select
#Your view code
我建议您阅读以下内容,Django forms
因为如果您需要更大的表格来管理具有许多字段的模型数据,则Django Form
可以节省大量时间
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句