1

I am trying to save data into the database from the website. My web page is taking the data (image from my system) but in the database, it's not showing up. If I add manually then it's creating a new user entry and storing it. Here are my files:

models.py

class Details(models.Model):
name = models.CharField(max_length=122)
username = models.CharField(max_length=122)
email = models.EmailField(max_length=122)
password = models.CharField(max_length=20)
def __str__(self):
    return self.name

class Image_Data(models.Model):
    img_User = models.ForeignKey(
        Details, blank=True, null=True, on_delete=models.CASCADE)
    img_password = models.ImageField(null=True, blank=True)

    def __str__(self):
    return str(self.img_password)

forms.py

class ImageForm(ModelForm):
class Meta:
    model = Image_Data
    fields = ('img_password',)
    labels = {
        'img_password': 'Add your image ',
    }
    widgets = {
        'img_password': forms.FileInput(attrs={'class': 'form-control', 'placeholder': 'Upload from device'})
    }

views.py

def register_new_b(request):
saved = False
if request.method == "POST":
    # take whatever is posted to the Details Form
    form = ImageForm(request.POST)
    if form.is_valid():
        form.save()
        messages.success(request, 'Your message has been sent!')
        return HttpResponseRedirect('/register_new_b?saved=True')
else:
    form = ImageForm()
    if 'saved' in request.GET:  # sends saved var in GET request
        saved = True

return render(request, 'register2.html', {'form': form, 'saved': saved})

Here is what I get in database upon saving the image

1 Answer 1

2

In the line:

form = ImageForm(request.POST)

you need to add request.FILES:

form = ImageForm(request.POST, request.FILES)

*Edit

An working example inside my own code: in views.py

def change_profile_pic(request):
    context = dict()
    if request.method == 'POST':
        form = ProfilePictureForm(request.POST, request.FILES)
        if form.is_valid():
            form.store(request.user.id)
            form = ProfilePictureForm()
            context['success_message'] = 'Picture changed'
            request.user.employee.refresh_from_db()
        else:
            context['error_message'] = 'Unable to change picture'
    else:
        form = ProfilePictureForm()
    context['form'] = form

    return render(request, 'template.html', context)

in forms.py

class ProfilePictureForm(forms.Form):
    image = forms.ImageField(required=True)

    def store(self, user_id):
        user = User.objects.get(id=user_id)
        user.employee.profile_picture = self.cleaned_data['image']
        user.employee.save()

in models.py

class Employee(models.Model):
    user = models.OneToOneField(User, blank=False, on_delete=models.CASCADE)
    profile_picture = models.ImageField(upload_to='profile_pictures', max_length=200, null=True, blank=True)

But I have also experimented with ModelForm, and this also saves the file correcly

Sign up to request clarification or add additional context in comments.

4 Comments

i tried this. But still not displaying the image name in the database
Looking agian (after my edit), I see that you are missing upload_to in your ImageField, not just the request.FILES in the form initalization
I tried adding a similar function as store() in your code. It throws an error : ImageForm' object has no attribute 'store'
As per my example, I created this function by my self and is not a standard form function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.