django - Ajax 选择下拉值并通过
问题描述
我正在尝试执行以下操作:用户将从下拉菜单中选择值,我想捕获数据并传递给值以检查它是否存在并过滤它。
使用 django 视图获取媒体数据文件列表。我想根据选定的数据站点进行过滤。
看法
@ajax_request
def listofiles(request, *args, **kwargs):
dir_name = "configurations"
template_name = 'loadlistofiles.html'
path = os.path.join(settings.MEDIA_ROOT, dir_name)
filelist = []
context = {'file': filelist}
for f in os.listdir(path):
if f.endswith(".pdf"): # to avoid other files
filelist.append("{}{}/{}".format(settings.MEDIA_URL, dir_name, f))
return render(request, template_name, {'file': filelist})
上面的代码将提供所有文件的列表。我想使用下拉选择过滤它
HTML AJAX
<script>
$(document).ready(function() {
$("#files").submit(function() { // catch the form's submit event
$.ajax({
url: 'loadlistofiles.html',
type: $(this).attr('GET'),
data: $(this).serialize(), // get the form data
success: function(data) { // on success..
$("#fetchdata").html(data); // update the DIV
}
});
return false;
});
});
</script>
如何根据下拉选择值过滤并检查文件中的字符串。例如: 文件名:dublin.aus1.pdf
Dropdown 会有一个类似的列表:AUS1, CH1, IND1 喜欢检查是否aus1
在文件中,如果是则只打印显示。
提前致谢。
解决方案
推荐阅读
- haskell - 将函数应用于列表中的每个元素到另一个列表中的每个元素
- python - Matplotlib venn3 7 个子集之一未显示
- javascript - 解析JSON后消失的元素
- tensorflow - 使用 tensorflow_model_server 和 ssl 配置
- java - 解析图像时出现错误:“org.apache.commons.imaging.ImageReadException:Jpeg 包含多个 Photoshop App13 段”
- google-maps - Flutter 谷歌地图,标记拖动
- c++ - c++ 多线程原子加载/存储
- php - 我的重定向是在 Apache2 服务器上上传网站后删除斜杠 - admin.site.comlogin 而不是 admin.site.com/login
- c# - 是否可以使用 linq 创建一个按给定位置排序的 csv 字符串?
- ios - 什么是开发吊舱?