django 后台上传图片

pipidi

zhujingdi1998@gmail.com

1.model upload_to指的是上传文件的位置

class HeadsCulpture(models.Model):
    img = models.ImageField(upload_to='img')
    name = models.CharField(max_length=20)
    belong=models.ForeignKey(User)

2.模板设置

<form method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                <input type="file" name="img"  onchange="f_change(this)">
                <button type="submit">上传</button>
            </form>

3.view 填写

if request.method == 'POST':  # 如果是post
            new_img = HeadsCulpture(
                img=request.FILES.get('img'),
                name=request.FILES.get('img').name,
                belong=request.user
            )
            new_img.save()

阅读量