将表单提交存储在特定文件夹{d}中的django ImageField中

时间:2018-06-07 12:32:09

标签: 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(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,
            )

这是请求中发送的内容:

enter image description here

这是我想要存储图像的路径。

enter image description here

更新:

这是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>

1 个答案:

答案 0 :(得分: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)