在Django用户更改形式中更改图像

在Django用户更改形式中更改图像

本文介绍了在Django用户更改形式中更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个django webapp,用户可以在其中上传个人资料图像。我还使用默认的 USERCHANGEFORM 创建了一个 EDIT PROFILE 页面。但是问题是,我无法从该表单更新个人资料图片。我可以删除它,但不能上传新的吗?需要帮助。
这是我的用户创建表格

I am making a django webapp where users can upload profile image. I have also created an EDIT PROFILE page by using the default USERCHANGEFORM. But the problem is, that I cannot update the profile picture from that form. I can delete it but not upload new one? Help needed.This is my USER CREATION FORM:


class SignUpForm(UserCreationForm):

    photo               = forms.ImageField(required=False)
    bio                 = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your bio'}))
    designation         = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your designation'}))
    university          = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your university'}))

    company_name        = forms.CharField(widget=forms.TextInput(attrs={'placeholder': "Enter your company's name"}))
    grad_year           = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'What year did you graduate in?'}))
    phone               = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your phone number'}))
    address             = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your present address'}))
    city                = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your city'}))
    company_category    = forms.CharField(widget=forms.TextInput(attrs={'placeholder':  "Enter your company's category"}))
    company_desc        = forms.CharField(widget=forms.TextInput(attrs={'placeholder': "Enter your company's description"}))
    company_site        = forms.CharField()
    no_employees        = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Enter no. of employees in your company'}))
    technologies        = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'What technologies are you interested in?'}))
    markets             = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'What markets are you interested in?'}))
    linkedin            = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your Linked In profile'}))

    def __init__(self, *args, **kwargs):
        super(UserCreationForm, self).__init__(*args, **kwargs)
        del self.fields['password2']

        for fieldname in ['password1']:
            self.fields[fieldname].help_text = None

        self.fields['password1'].widget.attrs.update({'placeholder': 'Enter your password'})
        self.fields['first_name'].widget.attrs.update({'placeholder': 'Enter your first name'})
        self.fields['last_name'].widget.attrs.update({'placeholder': 'Enter your last name'})
        self.fields['company_site'].widget.attrs.update({'placeholder': "Enter company's website"})
        for field in ['email',
                   'password1',
                   'first_name',
                   'last_name',
                   'bio',
                   'designation',
                   'university',
                   'company_name',
                   'grad_year',
                   'phone',
                   'photo',
                   'address',
                   'city',
                   'company_category',
                   'company_desc',
                   'company_site',
                   'no_employees',
                   'technologies',
                   'markets',
                   'linkedin']:

                   self.fields[field].widget.attrs['class'] = "col-lg-4 col-md-12 col-sm-12 col-xs-12 offset-lg-2 ml-6 form-control"
                   self.fields[field].widget.attrs['style'] = "padding:20px;"

        self.fields['photo'].widget.attrs['class'] = "col-lg-8 col-md-12 col-sm-12 col-xs-12 offset-lg-1 "


    class Meta:
        model = User
        widgets = {
        "email": forms.fields.TextInput(attrs={'placeholder':'Enter your email'}),
        }
        fields = ('email',
                   'password1',
                  'first_name',
                  'last_name',
                   'photo',
                   'bio',
                   'designation',
                   'university',
                   'company_name',
                   'grad_year',
                   'phone',
                   'address',
                   'city',
                   'company_category',
                   'company_desc',
                    'company_site',
                   'no_employees',
                   'technologies',
                   'markets',
                   'linkedin')


这是我的用户更改表

class EditProfileForm(UserChangeForm):

    def __init__(self, *args, **kwargs):
        super(UserChangeForm, self).__init__(*args, **kwargs)

        for field in [
                    'first_name',
                    'last_name',
                    'bio',
                    'designation',
                    'university',
                    'company_name',
                    'grad_year',
                    'phone',
                    'address',
                    'city',
                    'company_category',
                    'company_desc',
                    'company_site',
                    'no_employees',
                    'technologies',
                    'markets',
                    'linkedin']:
            self.fields[field].widget.attrs['class'] = "col-lg-8 col-md-12 col-sm-12 col-xs-12 offset-lg-2 ml-6 form-control"
            self.fields[field].widget.attrs['style'] = "padding:20px;"


        self.fields['photo'].widget.attrs['class'] = "col-lg-8 col-md-12 col-sm-12 col-xs-12 offset-lg-2 ml-6"
        self.fields['password'].widget.attrs['class'] = "hidden"


    class Meta:
        model = User
        fields = ('first_name',
        'last_name',
        "photo",
        "bio",
        "designation",
        "university",
        "company_name",
        "grad_year",
        "phone",
        "address",
        "city",
        "company_category",
        "company_desc",
         "company_site",
        "no_employees",
        "technologies",
        "markets",
        "linkedin"
        )```


推荐答案

基本上,您是在获取实例,但不仅可以更新图片。

Basically you are getting the instance but you are not only able to update only the picture.

这篇关于在Django用户更改形式中更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:59