Python 如何使用Post请求将html表单中的图像文件存储在Django中?

Python 如何使用Post请求将html表单中的图像文件存储在Django中?,python,django,django-models,django-views,django-templates,Python,Django,Django Models,Django Views,Django Templates,我正在做一个项目,需要一个个人资料图片。 我在Django UserProfile中创建了一个模型 class UserProfile(models.Model): user = models.OneToOneField( User, on_delete=models.CASCADE, primary_key=True) image = models.ImageField() adress = models.CharField(default='', max_length=150, bl

我正在做一个项目,需要一个个人资料图片。 我在Django UserProfile中创建了一个模型

class UserProfile(models.Model):
user = models.OneToOneField(
    User, on_delete=models.CASCADE, primary_key=True)
image = models.ImageField()
adress = models.CharField(default='', max_length=150, blank=True)
cnic = models.IntegerField(null=True)
contact = models.IntegerField(null=True)
city = models.CharField(max_length=50, null=True)
about = models.CharField(max_length=50, null=True)
location = models.CharField(max_length=150, null=True)
subscriptions = models.IntegerField(null=True)
rating = models.IntegerField(null=True)
我目前正在从HTML获取数据

<form action="/users/profile/" method="POST" style="display: contents;">
            {% csrf_token %}

            <div class="col-md-3 border-right">
                <div class="d-flex flex-column align-items-center text-center p-3 py-5">
                    {% if profile.image %}
                    <img class="rounded-circle mt-5" src="/media/{{profile.image}}"
                        style="width: 200px;max-height: 300px;" id='image'>
                    {% else %}
                    <img class="rounded-circle mt-5"
                        src="https://image.shutterstock.com/image-vector/house-not-available-icon-flat-260nw-1030785001.jpg"
                        style="width: 200px;max-height: 300px;" id='image'>
                    {% endif %}
                    <label for="upload-photo" class="uploadImgLabel btn btn-outline-danger w-75">Browse</label>
                    <input type="file" name="photo" id="upload-photo" required />

                    <span class="font-weight-bold">{{user}}</span><span
                        class="text-black-50">{{user.email}}</span><span>
                    </span>
                </div>

{%csrf_令牌%}
{%if profile.image%}
{%else%}
{%endif%}
浏览
{{user}}{{user.email}
所有其他字段都正常工作,我可以通过在views.py中打印来可视化数据

if request.method == 'POST':
    photo = request.POST.get('photo')
    fname = request.POST.get('firstname')
    lastname = request.POST.get('lastname')
    contact = request.POST.get('contact')
    address = request.POST.get('address')
    email = request.POST.get('email')
    country = request.POST.get('country')
    cnic = request.POST.get('cnic')
    city = request.POST.get('city')

    user = User.objects.get(username=request.user)
    user.first_name = fname
    user.last_name = lastname
    user.email = email
    user.save()

    obj = models.UserProfile.objects.get(user=request.user)
    obj.adress = address
    # obj.image = photo, <---- Here is the problem
    obj.contact = contact
    obj.city = city
    obj.cnic = cnic
    obj.subscriptions = 100
    obj.rating = 4
    obj.save()
if request.method==“POST”:
photo=request.POST.get('photo')
fname=request.POST.get('firstname')
lastname=request.POST.get('lastname')
contact=request.POST.get('contact')
地址=request.POST.get('地址')
email=request.POST.get('email')
country=request.POST.get('country')
cnic=request.POST.get('cnic')
city=request.POST.get('city')
user=user.objects.get(用户名=request.user)
user.first_name=fname
user.last_name=lastname
user.email=电子邮件
user.save()
obj=models.UserProfile.objects.get(user=request.user)
obj.address=地址

#obj.image=photo,我认为您需要使用request.FILES['photo']从表单访问上传的文件。
该文件不保存在数据库中,仅保存在路径中,并将上载到媒体根目录

图像上载分为两部分。 首先,表单的编码必须设置为“多部分/表单数据” 你可以很容易地做到这一点

enctype=“多部分/表单数据”

我们为什么要这样做?这是一个阅读更多关于它的好地方

现在,因为您的数据不像文本或JSON那样被发送,所以您不能使用普通的
请求.POST.get('photo')

您必须使用
request.FILES.get('photo')接收它。
您可以在Django中阅读更多关于它的信息

奖金-

如果您指定一个目录,那么在模型中定义图像列会工作得更好,类似这样

image=models.ImageField(上传到=“%Y/%m/%d”)