【发布时间】:2019-11-27 20:01:49
【问题描述】:
Django 和 Python 的新手。 创建带有下拉列表的导航栏(通过 form.py 填充) 用户选择项目后,将显示另一个下拉列表。 用户从列表中选择项目并点击提交后,喜欢触发 python 脚本获取数据并以表格格式填充 卡在执行 python 脚本
以下代码: 视图.py:
class StatusLayoutPageView(FormView):
template_name = "status_layout.html"
form_class = SelectLocationForm
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
return super().form_valid(form)
class DataView(FormView):
## This will contain the output
template_name = "data.html"
form_class = SelectLocationForm
这是models.py
LOCATIONS = (
('DC1', 'DC1'),
('DC2', 'DC2'),
)
class SelectLocationForm(forms.Form):
error_css_class = 'error'
location = forms.ChoiceField(choices=LOCATIONS, required=True)
class Meta:
model = SelectLocation
这是模板:
<form method="post">{% csrf_token %}
<select name="location">
<option selected>Select Location</option>
{% for val in form.location %}
<option value="{{ val }}"></option>
{% endfor %}
</select>
<p>
<button align="center" class="button execute" name="submit" value="submit">GO TO</button>
</form>
遇到的问题是如何根据加载页面来判断用户选择了哪个值。也可以通过 onclick 按钮将数据传递给 python 脚本以通过数据运行并以表格格式输出。
预期输出:
首页:
导航栏:主页 |日志 |选择应用程序['查找','删除']
用户选择FIND
已分页:带导航栏主页 |日志 |查找 ['查找','删除']
另一个下拉菜单:
选择位置 ['DC1'、'DC2'、'DC3'] 按钮:SUBMIT
一旦用户点击按钮,它将运行一个python脚本。
【问题讨论】:
标签: python django drop-down-menu dropdown