Omar Jandali
Omar Jandali

Reputation: 824

Storing a form submitted Image in a django ImageField within specific folder

I have a django model form. One of the fields in the form is a ImageField with a set path to a specific folder to store the images. I want to grab the image that is submitted with the form and name of the image. I want the image itself store in the folder and the name to store in the database to be able to grab it whenever I need it. I looked around and there were not usable answers to fixing this problem for me. Here is my code:

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,
            )

this is what is sent in the request:

enter image description here

Here is the directly with the path I want to store the images in.

enter image description here


UPDATE:

here is the models.py model:

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)

Here is the forms.py file:

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']

Here is the template:

Profile:

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

Upvotes: 0

Views: 825

Answers (1)

Du D.
Du D.

Reputation: 5310

The upload_to parameter refers to the relative path inside the "media" folder, it is not relative to the root path. To get this to work, first you have to specify the media root in your settings.py and then the path below will tell django to put all the uploaded images to /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)

In order for the form to save the images you have to provide your form with the files as well.

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

Upvotes: 1

Related Questions