基於form的django圖片上傳

第一種,直接上傳html

views
python

class TestimgForm(forms.Form):
    title = fields.CharField()
    img = fields.FileField()
def testimg(request):
    import os,time
    if request.method == 'GET':
        img = TestimgForm()
        return render(request, 'backend/testimg.html',{'img':img})
    else:
        obj = TestimgForm(request.POST, request.FILES)
	if obj.is_valid():
	    img = request.FILES.get('img')
            time = time.strftime('%Y%m%d%H%M%S')
             ext = os.path.splitext(img.name)[1]
	     file_path = os.path.join('upload/article',time+ext)
                #將圖片重命名
             f = open(file_path,'wb')
             for line in imgs.chunks():
             f.write(line)
             f.close()
	     return HttpResponse('上傳成功')

第二種,寫入數據庫數據庫

html
post

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
</head>
<body>
<form action="testimg" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ img }}
    <input type="submit" value="提交"/>
</form>
</body>
</html>

views
spa

class TestimgForm(forms.Form):
    title = fields.CharField()
    img = fields.FileField()
def testimg(request):
    import os,time
    if request.method == 'GET':
        img = TestimgForm()
        return render(request, 'backend/testimg.html',{'img':img})
    else:
        obj = TestimgForm(request.POST, request.FILES)
		if obj.is_valid():
		title = obj.cleaned_data['title']
            img = obj.cleaned_data['img']
        time = time.strftime('%Y%m%d%H%M%S')
        ext = os.path.splitext(img.name)[1]
		img.name = time + ext
            Imgtest.objects.create(title=title,img=img)
		return HttpResponse('上傳成功')

models.pyorm

class Imgtest(models.Model):
    title = models.CharField(verbose_name='標題',max_length=32,)
    img = models.ImageField(verbose_name='縮略圖',upload_to="./article/",
                            help_text="大小200*200,不超過200k",
                            default="/upload/article/common.jpg")

完整代碼csrf

class TestimgForm(forms.Form):
    title = fields.CharField()
    img = fields.FileField()
def testimg(request):
    import os,time
    if request.method == 'GET':
        img = TestimgForm()
        return render(request, 'backend/testimg.html',{'img':img})
    else:
        obj = TestimgForm(request.POST, request.FILES)
        if obj.is_valid():
        #(request.FILES['file'])
            # title = request.FILES.get('title')
            # img = request.FILES.get('img')
            title = obj.cleaned_data['title']
            img = obj.cleaned_data['img']
            time = time.strftime('%Y%m%d%H%M%S')
            ext = os.path.splitext(img.name)[1]
            img.name = time + ext
            # Imgtest.objects.create(title=title,img=img)
            # img是對象(文件大小,文件名稱,文件內容。。。)
            # file_path = os.path.join('upload/article',time+ext)
            file_path = os.path.join('upload/article', img.name)
            f = open(file_path,'wb')
            for line in img.chunks():
                f.write(line)
            f.close()
            return HttpResponse('...')
相關文章
相關標籤/搜索