1. 创建数据表
| class UserInfo(models.Model): |
| """用户信息""" |
| name = models.CharField(verbose_name="姓名", max_length=32) |
| age = models.IntegerField(verbose_name="年龄") |
| img = models.CharField(verbose_name="头像", max_length=128) |
| |
| |
| class CityInfo(models.Model): |
| """城市信息""" |
| name = models.CharField(verbose_name="名称", max_length=32) |
| count = models.IntegerField(verbose_name="人口") |
| |
| |
| |
| img = models.FileField(verbose_name="Logo", max_length=128, upload_to="city/") |
| |
| |
| |
2. 创建路由(url)
| from bbc_list.views import login, home_page, order, phone, admin, task, bill, chart, upload, city |
| urlpatterns = [ |
| |
| path("upload/list/", upload.upload_list), |
| path("upload/form/", upload.upload_form), |
| path("upload/model/form/", upload.upload_model_form), |
| |
| |
| path("city/list/", city.city_list), |
| path("city/add/", city.city_add),] |
3. 编写视图函数(views-upload.py)
| from django.http import HttpResponse |
| from django.shortcuts import render |
| |
| from bbc_list import models |
| |
| |
| def upload_list(request): |
| """上传文件""" |
| if request.method == "GET": |
| return render(request, "upload_list.html") |
| |
| |
| |
| |
| |
| file_object = request.FILES.get("avatar") |
| print(file_object.name) |
| |
| f = open(file_object.name, mode="wb") |
| for chunk in file_object.chunks(): |
| f.write(chunk) |
| f.close() |
| |
| return HttpResponse("...") |
| |
| |
| from django import forms |
| from bbc_list.utils.bootstrap import BootStrapForm |
| |
| |
| class UpForm(BootStrapForm): |
| bootstrap_exclude_fields = ["img"] |
| |
| |
| name = forms.CharField(label="姓名") |
| age = forms.CharField(label="年龄") |
| img = forms.FileField(label="头像") |
| |
| |
| def upload_form(request): |
| """form文件上传处理""" |
| title = "form文件上传" |
| if request.method == "GET": |
| form = UpForm() |
| return render(request, "upload_form.html", {"form": form, "title": title}) |
| |
| form = UpForm(data=request.POST, files=request.FILES) |
| if form.is_valid(): |
| |
| |
| |
| |
| image_object = form.cleaned_data.get("img") |
| import os |
| |
| |
| |
| |
| |
| |
| media_path = os.path.join("media", image_object.name) |
| |
| f = open(media_path, mode="wb") |
| for chunk in image_object.chunks(): |
| f.write(chunk) |
| f.close() |
| |
| |
| models.UserInfo.objects.create( |
| name=form.cleaned_data["name"], |
| age=form.cleaned_data["age"], |
| img=media_path |
| |
| ) |
| return HttpResponse("...") |
| return render(request, "upload_form.html", {"form": form, "title": title}) |
| |
| |
| from bbc_list.utils.bootstrap import BootStrapModelForm |
| |
| |
| class UpModelForm(BootStrapModelForm): |
| bootstrap_exclude_fields = ["img"] |
| |
| class Meta: |
| model = models.CityInfo |
| fields = "__all__" |
| |
| |
| def upload_model_form(request): |
| """modelForm处理文件上传""" |
| if request.method == "GET": |
| title = "modelform上传文件" |
| form = UpModelForm() |
| return render(request, "upload_form.html", {"form": form, "title": title}) |
| |
| form = UpModelForm(data=request.POST, files=request.FILES) |
| if form.is_valid(): |
| |
| |
| form.save() |
| return HttpResponse("成功") |
| return render(request, "upload_form.html") |
| |
4. 编写html页面
4.1 upload_list.html
| # upload_list.html |
| |
| {% extends "one.html" %} |
| |
| {% block content %} |
| <div class="container"> |
| |
| <form method="post" enctype="multipart/form-data"> |
| {% csrf_token %} |
| <input type="text" name="username"> |
| <input type="file" name="avatar"> |
| <input type="submit" value="提交"> |
| </form> |
| |
| </div> |
| |
| {% endblock %} |
| |
| {% extends "one.html" %} |
| {% block content %} |
| |
| <div class="container"> |
| <div class="panel panel-default"> |
| <div class="panel-heading"> |
| <h3 class="panel-title">{{ title }}</h3> |
| </div> |
| <div class="panel-body"> |
| |
| <form method="post" enctype="multipart/form-data" novalidate> |
| {% csrf_token %} |
| |
| {% for field in form %} |
| <div class="form-group"> |
| <label>{{ field.label }}</label> |
| |
| {{ field }} |
| <span style="color:red;"> {{ field.errors.0 }}</span> |
| </div> |
| {% endfor %} |
| |
| <button type="submit" class="btn btn-primary">提 交</button> |
| </form> |
| </div> |
| </div> |
| </div> |
| |
| {% endblock %} |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!