【问题标题】:Storing a form submitted Image in a django ImageField within specific folder将表单提交的图像存储在特定文件夹中的 django ImageField 中
【发布时间】:2018-11-17 08:59:20
【问题描述】:

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

模型.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>

【问题讨论】:

  • 您检查过cd['profile_pic'] 的值吗?
  • 第一张图片cd['profile_pic']profile_pic:MySplit_Logo_blue.png@EminBuğraSaral
  • 一个问题是你没有将文件传递给这样的表单:ProfileForm(request.POST, files=request.FILES) 所以我不确定它是返回一个字符串还是一个文件。
  • 另外,您需要确保在您的 html 表单定义中添加了 encytype="multipart/form-data",我总是忘记这一点。
  • 我是否在forms.py文件中添加encytype="multipart/form-data",并在图像字段描述中添加配置文件表单......@LuanFonseca

标签: django django-models django-forms django-rest-framework django-views


【解决方案1】:

upload_to 参数是指“media”文件夹内的相对路径,它与根路径无关。为了让它工作,首先你必须在你的 settings.py 中指定媒体根目录,然后下面的路径会告诉 django 把所有上传的图片放到 /media/images/profile_pics/。

upload_to Reference

profile_pic = models.ImageField(upload_to='images/profile_pics/', height_field=500, width_field=500, max_length=100)

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

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

【讨论】:

  • 我忘了添加我得到的错误我将更新我的帖子,我得到的错误在顶部
  • 是说the image field is required,是不是表示它没有识别图片或者是怎么回事...
  • 我现在收到以下错误:&lt;ul class="errorlist"&gt;&lt;li&gt;profile_pic&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; 我添加了request.FILES 我还更改了upload_to='images/profile_pics/...我需要在设置中做任何事情吗...跨度>
  • 您可以发布您的表单和您呈现表单的模板吗?
  • django是否会自动将生命保存在指定为upload_to的文件夹中,然后将链接保存在数据库中...
猜你喜欢
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多