Django-文件上传

定义:

上传规则-前端【html】:

上传规则-后端【Django】:

python 复制代码
urlpatterns = [
  
    path('upload',views.test_upload)
]

例子:

settings添加:

python 复制代码
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

主路由:

python 复制代码
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

分路由:

python 复制代码
urlpatterns = [
  
    path('upload',views.test_upload)
]

models:

python 复制代码
class Content(models.Model):
    title = models.CharField('标题', max_length=100)
    picture = models.FileField(upload_to='picture')

views:

python 复制代码
def test_upload(request):
    if request.method == 'POST':
        file = request.FILES.get('file')
        title = request.POST.get('title')
        Content.objects.create(title=title,file=file)
        return HttpResponse('上传成功')
    elif request.method == 'GET':
        return render(request,'node/test_upload.html')
相关推荐
小满zs4 小时前
Go语言第二十章(初探Gin)
后端·go
学长毕设4 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
小满zs4 小时前
Go语言第十九章(http标准库)
后端·go
岁月宁静4 小时前
从零拆解 AI 语音模拟面试系统
人工智能·python·agent
benchmark_cc4 小时前
个人开发者接入五档盘口:用 QuantDash 读取单只股票的验收清单与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
ZhengEnCi6 小时前
项目越写越乱?先别改架构:用一次目录重排 + 一份可复算的体检脚本把乱治住
python·架构·ai编程
考虑考虑6 小时前
nginx拦截url
运维·后端·nginx
Dmz丶6 小时前
前端转 AI 100 天 Day 45:阶段复盘——从“会调 API”到“做出 AI 产品”
python
GISer_Jing6 小时前
GitHub热点I
后端·ai·langchain
Sakura_j_h_7 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver