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 7623917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:49:37+00:00 2026-05-31T04:49:37+00:00

I have User form in which there is a username field. The fields in

  • 0

I have User form in which there is a username field. The fields in the form are initialized from the data in the db.

class PersonEditForm(forms.ModelForm):

    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.")})


class Meta:
    model = User

view.py

@login_required
def edit(request, template_name):

    if request.user.is_authenticated():
        if request.method == 'POST':
            form = PersonEditForm(request.POST)
            if form.is_valid():
                form.save(instance = request.POST, user = request.user)
                return HttpResponseRedirect(reverse('/success/'))

        else:
            user = User.objects.get(username = request.user)
            form = PersonEditForm(instance = user, initial = {'username': request.user.username})

            return render_to_response(template_name, {"form": form },
                              context_instance = RequestContext(request))

The logged in user can edit the username. The problem is while writing the clean_username method I would require the request.user, to check:

  1. if username == request.user.username #return true
  2. elif username != request.user.username

    a) User.objects.get(username = username) #if returns something then raise forms.ValidationError(_(“Username already taken.”))

    b) else user.username = username

How can I achieve this?

  • 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-05-31T04:49:39+00:00Added an answer on May 31, 2026 at 4:49 am

    You can get it in self.instance

    class PersonEditForm(forms.ModelForm):
        def clean_username(self):
            # self.instance is your current user
    
        ...
    

    A view should look like this

    if request.user.is_authenticated():
        if request.method == 'POST':
            form = PersonEditForm(request.POST, instance=request.user)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(reverse('/success/'))
    
        else:
            form = PersonEditForm(instance=request.user, initial={'username': request.user.username})
    
        return render_to_response(template_name, {"form": form },
                              context_instance = RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form in which the user can choose a component type from
I have table user which have fields username,password, and type. The type can be
I have table user which have fields username , password , and type .
I have a sign-in-action-form page which accept username and password from sign-in-form. Now most
I have a form which takes both the user details and an image uploaded
I have got a form which a user can use to create a new
I have a form which have many check boxes in it. User will check
I have an ASP.NET web form which I am adding a variable number User
I have a form in which the user selects a few items to display
I have a form allowing a user to signup for a news letter which

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.