原始的文件上传
form_test\urls.py
from django.conf.urls import url
from django.urls import re_path
from form_test import views
urlpatterns = [
url(r'^first/$', views.page_form_first),
# 用户登录
url(r'^user/login$', views.user_login, name='user_login'),
# 用户注册
url(r'^user/register$', views.user_register, name='user_register'),
# 从模型创建表单
url(r'^user/edit$', views.user_edit, name='user_edit'),
# 文件上传
url(r'^file/upload/origin$', views.file_upload_origin, name='file_upload_origin'),
]
form_test\views.py
def file_upload_origin(request):
""" 原始的文件上传 """
if request.method == 'POST':
# 获取上传的文件对象 二进制
file = request.FILES.get('avatar',None)
filename = os.path.join(settings.MEDIA_ROOT, 'test.jpg')
with open(filename, 'wb+') as f:
for chunk in file.chunks():
f.write(chunk)
print('上传成功')
return render(request,'file_upload_origin.html',{
})
form_test\templates\file_upload_origin.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>原始的文件上传</title>
</head>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="avatar">
<input type="submit" value="开始上传">
</form>
</body>
</html>
结合ORM模型文件上传
form_test\urls.py
from django.conf.urls import url
from django.urls import re_path
from form_test import views
urlpatterns = [
url(r'^first/$', views.page_form_first),
# 用户登录
url(r'^user/login$', views.user_login, name='user_login'),
# 用户注册
url(r'^user/register$', views.user_register, name='user_register'),
# 从模型创建表单
url(r'^user/edit$', views.user_edit, name='user_edit'),
# 文件上传
url(r'^file/upload/origin$', views.file_upload_origin, name='file_upload_origin'),
url(r'^file/upload/form$', views.file_upload_form, name='file_upload_form'),
]
form_test\forms.py
class AvatarUploadForm(forms.Form):
""" 头像上传form """
remark = forms.CharField(label='备注', max_length=32)
avatar = forms.ImageField(label='头像')
form_test\views.py
def file_upload_form(request):
""" 使用form表单实现文件上传 """
if request.method == 'POST':
form = AvatarUploadForm(request.POST, request.FILES)
if form.is_valid():
# 保存备注
# 保存图片
file = request.FILES.get('avatar', None)
filename = os.path.join(settings.MEDIA_ROOT, 'test2.jpg')
with open(filename, 'wb+') as f:
for chunk in file.chunks():
f.write(chunk)
print('上传成功')
else:
form = AvatarUploadForm()
return render(request,'file_upload_form.html',{
'form':form
})
form_test\templates\file_upload_form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>使用form表单实现文件上传</title>
</head>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<input type="submit" value="开始上传">
{{ form.as_p }}
</form>
</body>
</html>
结合ORM模型文件上传(二)
form_test\urls.py
from django.conf.urls import url
from django.urls import re_path
from form_test import views
urlpatterns = [
url(r'^first/$', views.page_form_first),
# 用户登录
url(r'^user/login$', views.user_login, name='user_login'),
# 用户注册
url(r'^user/register$', views.user_register, name='user_register'),
# 从模型创建表单
url(r'^user/edit$', views.user_edit, name='user_edit'),
# 文件上传
url(r'^file/upload/origin$', views.file_upload_origin, name='file_upload_origin'),
url(r'^file/upload/form$', views.file_upload_form, name='file_upload_form'),
url(r'^file/upload/weibo$', views.file_upload_weibo, name='file_upload_weibo'),
]
form_test\forms.py
class WeiboImageForm(forms.ModelForm):
""" 微博图片上传 """
content = forms.CharField(label='微博内容', max_length=256,widget=forms.Textarea(attrs={
'placeholder':'请输入微博内容'
}))
class Meta:
model = WeiboImage
fields = ['image']
def save(self, user, commit=False):
# 先不保存到数据库,先得到对象,接下来对这个对象进行修改,然后在保存
obj = super().save(commit)
data = self.cleaned_data
content = self.cleaned_data['content']
# 1. 创建微博的记录
weibo = Weibo.objects.create(user=user, content=content)
# 2. 修改微博关联关系
obj.weibo = weibo
obj.save()
return obj
form_test\views.py
def file_upload_weibo(request):
""" 发送微博带图片 """
user = WeiboUser.objects.get(pk=1)
if request.method == 'POST':
form = WeiboImageForm(request.POST, request.FILES)
if form.is_valid():
obj = form.save(user, False)
print('保存成功',obj.pk)
else:
form = WeiboImageForm()
return render(request, 'file_upload_weibo.html',{
'form':form
})
form_test\templates\file_upload_weibo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>带图片的微博发布</title>
</head>
<body>
<h3>发布微博</h3>
<form action="#" method="post" enctype="multipart/form-data">
{{ form.as_p }}
<input type="submit" value="发布微博">
</form>
</body>
</html>