这里记录使用pillow
测试环境
- Python版本:python3
- 用到的三方库
django
: 2.1.7
pymysql
: 0.9.3
pillow
:
基本工作
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'HOST': '127.0.0.1',
'PASSWORD': '123456',
'POST': '3306',
'AUTOCOMMIT': True,
}
}
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
- 项目目录下(
__init__.py
)配置mysql
驱动
import pymysql
pymysql.install_as_MySQLdb()
from django.urls import path,include,re_path
from my_file import views
from hello.settings import MEDIA_ROOT, MEDIA_URL,STATICFILES_DIRS
from django.contrib.staticfiles.urls import static
from django.views.static import serve
urlpatterns = [
path('file/', include(('my_file.urls','file'),namespace='file')),
path('',views.hello),
re_path(r'^static/(?P<path>.*)$', serve, {"document_root": STATICFILES_DIRS[0]}),
re_path(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),
]
urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
- 创建相关一应用
pyhton manage.py startapp 应用名
示例:
pyhton manage.py startapp my_file
上传图片
from django.db import models
class Img(models.Model):
img_url = models.ImageField(upload_to='img')
- 创建数据表(这里使用django中的迁移命令)
- 生成迁移文件:
python manage.py makemigrations
- 迁移到数据库:
python manage.py migrate
- 视图函数
views.py
from django.shortcuts import render
from my_file.models import Img,My_file
def upload_file(request):
if request.method == 'GET':
return render(request, 'up_file.html')
if request.method == 'POST':
my_file = request.FILES.get('my_file')
if not my_file:
return render(request, 'up_file.html', {'msg': '没有文件...请从新上传'})
img = Img(img_url=my_file)
img.save()
return render(request, 'up_file.html', {'msg': '上传成功'})
html
文件
示例:这里from表单上传,也可以使用ajax异步提交(略)
<form action="/file/my_file/" method="post" ENCTYPE="multipart/form-data">
{% csrf_token %}
<input type="file" name="my_file">
<br>
<input type="submit" value="upload" id="show_tip" >
</form>
文件上传
class File(models.Model):
file_path = models.FileField(upload_to='file')