怎样设计个人网站,长沙短视频制作公司,网页建设方案怎么写,wordpress仿雷锋网1. 项目结构
myproject/
├── myproject/
│ ├── settings.py
│ ├── urls.py
│ └── ...
├── myapp/
│ ├── templates/
│ │ └── upload.html
│ ├── views.py
│ ├── urls.py
│ └── ...
└── media/ # 手动创…1. 项目结构
myproject/
├── myproject/
│ ├── settings.py
│ ├── urls.py
│ └── ...
├── myapp/
│ ├── templates/
│ │ └── upload.html
│ ├── views.py
│ ├── urls.py
│ └── ...
└── media/ # 手动创建此目录 2. 配置Django设置settings.py
# settings.py
import osINSTALLED_APPS [myapp, # 确保应用已注册# ...
]MEDIA_URL /media/
MEDIA_ROOT os.path.join(BASE_DIR, media)# 允许大文件上传可选
DATA_UPLOAD_MAX_MEMORY_SIZE 52428800 # 50MB 3. 路由配置
项目路由myproject/urls.py
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import staticurlpatterns [path(, include(myapp.urls)),
] static(settings.MEDIA_URL, document_rootsettings.MEDIA_ROOT)
应用路由myapp/urls.py
from django.urls import path
from . import viewsurlpatterns [path(, views.upload_view, nameupload),path(upload/, views.file_upload, namefile_upload),
] 4. 视图逻辑myapp/views.py
from django.shortcuts import render
from django.http import JsonResponse
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
import osdef upload_view(request):return render(request, upload.html)csrf_exempt # 临时禁用CSRF生产环境需修复
def file_upload(request):if request.method POST and request.FILES.get(file):uploaded_file request.FILES[file]save_path os.path.join(settings.MEDIA_ROOT, uploaded_file.name)# 分块写入文件with open(save_path, wb) as destination:for chunk in uploaded_file.chunks():destination.write(chunk)return JsonResponse({status: success,filename: uploaded_file.name})return JsonResponse({status: error}, status400) 5. 前端模板myapp/templates/upload.html
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8!-- Bootstrap 5 --link hrefhttps://cdn.jsdelivr.net/npm/bootstrap5.1.3/dist/css/bootstrap.min.css relstylesheettitle文件上传/title
/head
bodydiv classcontainer mt-5 stylemax-width: 600px;h2 classmb-4文件上传演示/h2!-- 文件选择 --div classmb-3input typefile classform-control idfileInput/div!-- 进度条 --div classprogress mb-3 styleheight: 25px; display: none; idprogressContainerdiv idprogressBar classprogress-bar progress-bar-striped roleprogressbar stylewidth: 0%0%/div/div!-- 状态提示 --div idstatusMessage/div!-- 上传按钮 --button classbtn btn-primary onclickstartUpload()开始上传/button/div!-- 依赖库 --script srchttps://code.jquery.com/jquery-3.6.0.min.js/scriptscript srchttps://cdn.jsdelivr.net/npm/bootstrap5.1.3/dist/js/bootstrap.bundle.min.js/scriptscriptfunction startUpload() {const fileInput document.getElementById(fileInput);const file fileInput.files[0];if (!file) {showMessage(请先选择文件!, danger);return;}const formData new FormData();formData.append(file, file);// 显示进度条$(#progressContainer).show();$.ajax({url: /upload/,type: POST,data: formData,contentType: false, // 必须设置processData: false, // 必须设置xhr: function() {const xhr new window.XMLHttpRequest();// 进度事件监听xhr.upload.addEventListener(progress, function(evt) {if (evt.lengthComputable) {const percent Math.round((evt.loaded / evt.total) * 100);updateProgress(percent);}}, false);return xhr;},success: function(response) {showMessage(文件 ${response.filename} 上传成功!, success);resetUI();},error: function(xhr) {showMessage(上传失败: (xhr.responseJSON?.status || 服务器错误), danger);resetUI();}});}function updateProgress(percent) {$(#progressBar).css(width, percent %).text(percent %);}function showMessage(text, type) {const alertClass alert alert-${type} alert-dismissible fade show;$(#statusMessage).html(div class${alertClass} rolealert${text}button typebutton classbtn-close data-bs-dismissalert/button/div);}function resetUI() {setTimeout(() {$(#progressContainer).hide();updateProgress(0);}, 1500);}/script
/body
/html
运行 HTML 6. 运行步骤
创建媒体目录
mkdir media
启动开发服务器
python manage.py runserver访问 http://localhost:8000 测试上传功能 关键功能说明
进度条实现 使用XMLHttpRequest的progress事件监听上传进度动态更新Bootstrap进度条的宽度和文本
安全增强生产环境必做 移除csrf_exempt装饰器在前端添加CSRF Token
// 在AJAX请求中添加headers
headers: {X-CSRFToken: document.querySelector([namecsrfmiddlewaretoken]).value
},
文件处理 使用file.chunks()分块处理大文件保存到MEDIA_ROOT指定目录
用户体验优化 上传完成后的自动状态重置可关闭的Bootstrap提示组件进度条动画效果条纹动画
常见问题解决
进度条不更新 检查浏览器控制台是否有CORS错误确认xhr.upload.addEventListener正确绑定
文件保存失败 确保MEDIA_ROOT目录存在且有写入权限检查Django的settings.py配置
CSRF验证失败 在生产环境中务必处理CSRF Token在模板中添加{% csrf_token %} 通过以上步骤您可以在Django中实现一个带Bootstrap进度条的文件上传功能既保证基本功能又具备良好的用户体验。 文章转载自: http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn http://www.morning.tmjhy.cn.gov.cn.tmjhy.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.krgjc.cn.gov.cn.krgjc.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.mtsgx.cn.gov.cn.mtsgx.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.mhnrx.cn.gov.cn.mhnrx.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.bdtpd.cn.gov.cn.bdtpd.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.tkgjl.cn.gov.cn.tkgjl.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.lczxm.cn.gov.cn.lczxm.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn