Django文件上传,供大家参考,具体内容如下
需求
1、完成学生信息注册操作
2、将学生信息入库
3、将上传文件存放至项目下media文件夹下
4、显示所有学生信息
创建模型类
1
2
3
4
5
6
7
8
9
10
|
class Student(models.Model): sno = models.AutoField(primary_key = True ) sname = models.CharField(max_length = 30 ) photo = models.ImageField(upload_to = 'imgs' ) <! - - 内部类写法 数据库中的名字 - - > class Meta: db_table = 't_stu' def __str__( self ): return self .sname |
settings.py文件中文件上传相关设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
INSTALLED_APPS = [ ... 'stu' ] DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.mysql' , 'NAME' : 'django22' , 'USER' : 'root' , 'PASSWORD' : '123321' , 'HOST' : '127.0.0.1' , 'PORT' : '3306' , } } MEDIA_URL = '/media/' <! - - 设置MEDIA_ROOT 默认为空 模型类中图片上传地址 MEDIA_ROOT + up_load - - > <! - - BASE_DIR 为项目录 - - > MEDIA_ROOT = os.path.join(BASE_DIR, 'media' ) |
映射数据库表
1
2
3
|
#在终端中敲命令 python manage.py makemigrations test python manage.py migrate |
配置URL
主路由
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from django.contrib import admin from django.urls import path, re_path,include from djurls.settings import MEDIA_ROOT from stu import urls from . import views #配置路由读取后台上传文件 from django.views.static import serve urlpatterns = [ path( 'test/' ,include( 'test.urls' )), re_path(r '^media/(?P<path>.*)/$' , serve, { "document_root" : MEDIA_ROOT}), #server 视图函数 将MEDIA的路径和正则匹配的模板路径 显示图片 |
子路由
1
2
3
4
5
6
7
8
9
10
|
from django.urls import path from test import views urlpatterns = [ path( 'test/' ,views.index.as_view()), path( 'show/' ,views.show) ] |
创建视图
stu/views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import os from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.shortcuts import render from django.views import View from djurls.settings import BASE_DIR from test.models import Student <! - - 通过as_view处理自动获取请求方式 - - > class index(View): def get( self ,request): return render(request, 'load.html' ) def post( self ,request): name = request.POST.get( 'sname' ,'') photo = request.FILES.get( 'photo' ,'') age = request.POST.get( 'age' ,'') <! - - 进行校验 将文件名的后缀字符串分割 判断 - - > extenedname = photo.name[photo.name.rindex( '.' ) + 1 :] allowedname = [ 'jpg' , 'png' ] if extenedname not in allowedname: return Http404() stu = Student.objects.create(sname = name,age = 20 ,photo = photo) if stu: return HttpResponse( '注册成功' ) else : return HttpResponseRedirect( '/test/test/' ) def show(request): stulist = Student.objects. all () return render(request, 'show.html' ,{ 'stulist' :stulist}) |
创建模板
templates/index.html 注册界面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >Title</ title > </ head > < body > < form action = "/test/test/" method = "post" enctype = "multipart/form-data" > {% csrf_token %} < p >姓名< input type = "text" name = "sname" ></ p > < p >年龄 < input type = "number" name = "age" ></ p > < p >照片 < input type = "file" name = "photo" ></ p > < input type = "submit" value = "注册" > </ form > </ body > </ html > |
show.html 显示数据 加载图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >Title</ title > </ head > < body > < table border = "1px solid black" cellspacing = "0px" width = "500px" align = "center" > < tr height = "100px" align = "center" > < td >编号</ td > < td >姓名 </ td > < td >年龄</ td > < td >头像</ td > </ tr > {% for stu in stulist %} < tr height = "100px" align = "center" > < td >{{ forloop.counter }} </ td > < td >{{ stu.sname }}</ td > < td >{{ stu.age }}</ td > < td >< img src = "/media/{{ stu.photo }}" alt = "" ></ td > <!--读取photo的路径 在主路由访问 通过server处理并显示--> </ tr > {% endfor %} </ table > </ body > </ html > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Chujianlong321/article/details/91985412