![](/img/trans.png)
[英]Django 1.9.4 Get input data from form to show into a different template
[英]How to get data from form and show in dropdown in django
我正在尝试做的事情:
上传 CSV 文件(表格 #1)并在下拉列表(表格 #2)中显示 CSV 文件的 header。 这些 forms 在同一页上。
我试过的:
现在我可以上传 CSV 文件并在网页中显示 header 。
索引.html
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<label for="file1">Upload Files</label>
<div class="custom-file">
<input type="file" accept=".csv" id="file1" name="file" required="True" class="form-control custom-file-input">
<label class="custom-file-label" for="file1"></label>
</div>
</div>
<div class="form-group d-flex justify-content-center">
<button type="submit" class="btn text-white w-50" value="Upload">Upload</button>
</div>
</form>
视图.py
def read_csv(request):
csv_file = request.FILES['file']
data = pd.read_csv(csv_file)
i = list(data.head(0))
context = {'loaded_data': i}
return render(request, "WebApp/index.html", context)
所以数据在一个列表中,我只需要遍历<option>
:
<select class="custom-select" id="inputGroupSelect01">
{% for x in loaded_data %}
<option value="{{ x }}">
{{ x }}
</option>
{% endfor %}
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.