Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:51:58+00:00 2026-06-05T17:51:58+00:00

I’ve a user profile and would like allow the user update everything about himself(username,

  • 0

I’ve a user profile and would like allow the user update everything about himself(username, password, name and others informations). With the code below, the password1 and password2 fields aren’t loaded and the clean_username method is not called. To complete, when I call is_valid method it always return False but with no errors.

Could anybody help me? Thank you.

#forms.py
class UserProfileForm(ModelForm):
    c_user = None
    error_messages = {
        'duplicate_username': _("A user with that username already exists."),
        'password_mismatch': _("The two password fields didn't match."),
    }
    username = forms.RegexField(label=_("Username"), max_length=30,
        regex=r'^[\w.@+-]+$',
        help_text = _("Required. 30 characters or fewer. Letters, digits and "
                      "@/./+/-/_ only."),
        error_messages = {
            'invalid': _("This value may contain only letters, numbers and "
                         "@/./+/-/_ characters.")})

    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)
        if kwargs.has_key('instance'):
            current_user = kwargs.get('instance').user
            self.c_user = current_user
            self.password1 = forms.CharField(label=_("Password"),
                widget=forms.PasswordInput, required = False)
            self.password2 = forms.CharField(label=_("Password confirmation"),
                widget=forms.PasswordInput, required = False,
                help_text = _("Enter the same password as above, for verification."))
        else: 
            self.password1 = forms.CharField(label=_("Password"),
                widget=forms.PasswordInput)
            self.password2 = forms.CharField(label=_("Password confirmation"),
                widget=forms.PasswordInput,
                help_text = _("Enter the same password as above, for verification."))

    def clean_username(self):
        # Since User.username is unique, this check is redundant,
        # but it sets a nicer error message than the ORM. See #13147.
        username = self.cleaned_data["username"]
        if (self.c_user == None) or (self.c_user.username != username):
            try:
                User.objects.get(username=username)
            except User.DoesNotExist:
                return username
            raise forms.ValidationError(self.error_messages['duplicate_username'])

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1", "")
        password2 = self.cleaned_data["password2"]
        if password1 != password2:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'])
        return password2

    class Meta:
        model = UserProfile
        exclude=('user')

>

#views.py

@csrf_protect
def update_user(request):
    user = request.user
    user_profile = user.get_profile()
    if request.method == 'GET':
        initials = {'username':user.username,
                'email':user.email,
                'name':user_profile.name,
                'birth_date':user_profile.birth_date,
                'weight':user_profile.weight,
                'height':user_profile.height,
                'smoke':user_profile.smoke,
                'drink_alcohol':user_profile.drink_alcohol,
                'alergies':user_profile.alergies,
                }
        form = UserProfileForm(initial=initials)
    elif request.method == 'POST':
        form = UserProfileForm(request.POST, instance=user_profile)
        if form.is_valid():
            f = form.save(commit=False)
            user.username = request.POST.get('username')
            user.save()
            f.user = user
            f.save()
            print request.POST.get('username')
            return redirect('/')
    return render_to_response('profile.html', {'form':form}, context_instance=RequestContext(request))

UPDATE: Thank you Daniel Roseman! Now I’m having problem just with the fields password1 and password2. They aren’t being loaded.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:51:59+00:00Added an answer on June 5, 2026 at 5:51 pm

    Try it:

    self.fields['password1'] = forms.CharField(label=_("Password"),
                widget=forms.PasswordInput, required = False)
    self.fields['password2'] = forms.CharField(label=_("Password confirmation"),
                widget=forms.PasswordInput, required = False,
                help_text = _("Enter the same password as above, for verification."))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.