将表单提交的图像存储在特定文件夹中的django ImageField中

将表单提交的图像存储在特定文件夹中的django ImageField中,django,django-models,django-forms,django-rest-framework,django-views,Django,Django Models,Django Forms,Django Rest Framework,Django Views,我有一张django模型表格。表单中的一个字段是ImageField,它具有到存储图像的特定文件夹的设置路径。我想抓取的形式和图像名称提交的图像。我希望图像本身存储在文件夹中,存储在数据库中的名称能够在我需要时抓取它。我环顾四周,没有可用的答案来解决这个问题。这是我的密码: Models.py: user = models.ForeignKey(User, on_delete=models.CASCADE) profile_pic = models.ImageField(uplo

我有一张django模型表格。表单中的一个字段是ImageField,它具有到存储图像的特定文件夹的设置路径。我想抓取的形式和图像名称提交的图像。我希望图像本身存储在文件夹中,存储在数据库中的名称能够在我需要时抓取它。我环顾四周,没有可用的答案来解决这个问题。这是我的密码:

Models.py:

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    profile_pic = models.ImageField(upload_to='static/images/profile_pics/', height_field=500, width_field=500, max_length=100)
    bio = models.TextField()
views.py:

        if request.method == "POST":
        form = ProfileForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            first_name = cd['first_name']
            last_name = cd['last_name']
            profile_pic = cd['profile_pic']
            bio = cd['bio']
            new_profile = Profile.objects.create(
                first_name = first_name,
                last_name = last_name,
                bio = bio,
                profile_pic = profile_pic,
                dob = dob,
            )
这是请求中发送的内容:

下面是我想要存储图像的路径


更新:

以下是models.py模型:

class Profile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    profile_pic = models.ImageField(upload_to='images/profile_pics/', max_length=100)
    bio = models.TextField()
    dob = models.DateField(auto_now=False, auto_now_add=False)
    phone = models.IntegerField()
    gender = models.CharField(max_length=11)
    company = models.CharField(max_length=22)
    occupation = models.CharField(max_length=22)
    street = models.CharField(max_length=44)
    city = models.CharField(max_length=22)
    state = models.CharField(max_length=4)
    zip_code = models.IntegerField()
    group = models.CharField(max_length=22)
    premium = models.BooleanField(default=False)
    active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
以下是forms.py文件:

class ProfileForm(forms.ModelForm):
    first_name = forms.CharField(max_length=22,
        label="First Name")
    last_name = forms.CharField(max_length=22,
        label="Last Name")
    dob = forms.DateField(
        label='Date of Birth',
        widget=forms.widgets.DateInput(attrs={'type':'date'})
    )
    gender_choices = (('M', 'Male'),
                      ('F', 'Female'),
                      ('O', 'Other'))
    gender = forms.TypedChoiceField(
        choices = gender_choices,
        label='Gender')
    class Meta:
        model = Profile
        fields = ['first_name', 'last_name', 'profile_pic', 'bio', 'dob', 'company',
                  'occupation', 'gender', 'phone', 'street', 'city', 'state', 'zip_code']
以下是模板:

简介:

<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" name="complete" value="complete">
</form>

{%csrf_令牌%}
{{form.as_p}}

上传至参数指的是“媒体”文件夹内的相对路径,而不是根路径。要使其正常工作,首先必须在settings.py中指定媒体根目录,然后下面的路径将告诉django将所有上载的图像放入/media/images/profile_pics/

为了让表单保存图像,您还必须为表单提供文件

# this way the form have access to the file uploaded in the buffer
form = ProfileForm(request.POST, request.FILES)    

您是否检查了
cd['profile\u pic']
的值?这是第一张图像
cd['profile\u pic']
profile\u pic:MySplit\u Logo\u blue.png
@EminBuğraSaralOne问题是您没有像这样将文件传递到表单:
ProfileForm(request.POST,files=request.files)
因此,我不确定它是否只返回字符串或文件。此外,您需要确保在html表单定义中添加了
encytype=“multipart/form data”
,我总是忘记了这一点。我是否在forms.py文件中添加
encytype=“multipart/form data”
,并在imagefield描述中添加配置文件表单@LuanFonsecai忘记添加我得到的错误,我要用我在顶部得到的错误更新我的帖子。它是说,
图像字段是必需的
,这是否意味着它无法识别图像或正在发生什么…我现在得到以下错误:
  • profile\u pic
    • 此字段是必需的。
我添加了
请求。文件
我还将
上载更改为
。。。我需要在设置中做些什么吗…你能发布你的表单和你呈现表单的模板吗?django是否会自动将生命保存在指定为upload_to的文件夹中,然后将链接保存在数据库中。。。
# this way the form have access to the file uploaded in the buffer
form = ProfileForm(request.POST, request.FILES)