开发者

Assign image to a form field in django.views

开发者 https://www.devze.com 2022-12-07 20:27 出处:网络
I am in the middle of a project and I am stuck. I have a edit profile feature where a user can edit their profile. Everytime a profile is created with an empty profile_picture field a default value is

I am in the middle of a project and I am stuck. I have a edit profile feature where a user can edit their profile. Everytime a profile is created with an empty profile_picture field a default value is provided. But once an image is assigned to field and then deleted. The field gets blank. What I want is everytime the field becomes None I want to reassign the default image by mentioning the file path.

Heres My view where I save the model form:-

           if form.is_valid():
                form.save(commit=False)

                print(form.cleaned_data)
                if 'avatar' in form.cleaned_data:

                    if form.cleaned_data.get('avatar') is False:
                       开发者_StackOverflow中文版 form.cleaned_data['avatar'] = image

                form.save() 

I want to mention the image path in place of image. Also suggest me some better ways for doing this.


if form.is_valid(): form.save(commit=False)

            print(form.cleaned_data)
            if 'avatar' in form.cleaned_data:

                if form.cleaned_data.get('avatar') is False:
                    form.cleaned_data['avatar'] = image

            form.save() 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号