Home > Blockchain >  How can I implement an edit profile page?
How can I implement an edit profile page?

Time:04-01

I'm trying to make my view for edit profile, but it doesn't work when saving data, I don't know what I'm doing wrong if it should work like a normal form, right? The form can be seen in my template, the problem is when saving, it doesn't save anything

views.py

def EditProfilePageView(request):
    # dictionary for initial data with
    # field names as keys
    context = {}

    # add the dictionary during initialization
    form = RegisterForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('profile')


    context['form'] = form
    return render(request, 'Usuarios/edit-profile.html', context)

form.py


class RegisterForm(UserCreationForm):
    username=forms.CharField(label="Usuario",widget=forms.TextInput(attrs={'class': 'form-control'}))
    email=forms.EmailField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    phone1=forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    phone2=forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    fax = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    email = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    website = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    socialMedia1 = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    socialMedia2 = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    socialMedia3 = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    alternativeContact = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    country = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    address = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    city = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    state = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
    zip = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))




    password1 = forms.CharField(
        label="Contraseña",
        widget=forms.PasswordInput(attrs={'class':'form-control', 'type':'password', 'align':'center'}),
    )
    password2 = forms.CharField(
        label="Confirma Contraseña",
        widget=forms.PasswordInput(attrs={'class':'form-control', 'type':'password', 'align':'center'}),
    )


    class Meta:
        model=User
        fields=['phone1','phone2','fax','email','website','socialMedia1','socialMedia2',
                'alternativeContact','country','address','city','state','zip']
        widgets = {
            'password': forms.TextInput(attrs={'class': 'form-control'}),
        }


CodePudding user response:

Bad news, you can't use the UserCreationForm model to save custom fields such as city, zip, ...etc

the UserCreationForm model has designed to work with a normal User model fields that has built in Django such as:

fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )

therefore, you can't create or put additional fields on it which means behind the scene the form in view will not receive the rest fields you added which will raise an error for some required fields because you tried to add null data for the required field. even if you added the data into these fields but these fields will not be added in the form.

you can give up on handling the UserCreationForm model then, you can customize your own forms by ModelForm to achieve your goal

  • Related