Django Forms Help needed

后端 未结 1 1947
余生分开走
余生分开走 2021-01-03 16:20

Im new to django and trying to make a user registration form with few validations. Apart from this I also want a username suggestion code which will tell the user if the use

相关标签:
1条回答
  • 2021-01-03 17:02

    Check out the django-registration application. And have a look at the Class registration.forms.RegistrationForm and their method clean_username.

    It should be easy to extend the form to suggest some usernames.

    here is some sample code to generate unique username with numbered postfixes:

        username # filled with user input or first/lastname etc.
    
        #check for other profile with equal names (and those with a postfix)
        others = [int(username.replace(name, "0")) 
                  for p in User.objects.filter(username__startswith=username).exclude(user=self.user)
                  if username.replace(name, "0").isdigit()]
    
        #do we need a postfix
        if len(others) > 0 and 0 in others:
            username = "%s%d" % (username, max(others) + 1)
    

    you could fill the generated names in a Form Choice Field: http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

    0 讨论(0)
提交回复
热议问题