我想在上传前将上传的文件大小限制在 5 MB 以下

I want to limit file size upload below 5 mb before uploading

我正在尝试在 django 的 jinja2 模板中检查上传的文件大小。我想在上传之前检查它,下面的代码是一个表单,点击按钮后将提交。它将在浏览器中上传数据集。

<form method="POST" class="post-form" enctype="multipart/form-data">
   {{ form.as_p }}           
   <button onclick="onSubmitBtn()" type="submit" id="btn-hide" class="btn btn-danger">Upload</button>                
</form>

如果您的意思是要在浏览器启动通过网络上传之前检查client-side/browser,那么您需要JavaScript。参见 this question and the accepted answer

更新(来自评论的附加信息):

绝对有可能编写一些与 {{ form.as_p }} 的渲染输出一起工作的 JavaScript。这 MDN doc should help. Also you'll probably need to do a preventDefault() 实际阻止 file/form 在您的条件不满足时提交。