2010-04-27 75 views
1

Im新的django和試圖做一個用戶註冊表,幾個驗證。 除此之外,我還希望有一個用戶名建議代碼,告訴用戶他正在嘗試註冊的用戶名是否可用或已經在使用中。那麼它應該提供一些可供選擇的建議。任何可能在相同或相同項目上工作的人都可以幫助我解決這個問題。Django Forms需要幫助

感謝

回答

1

退房的django-registration應用。並查看Class registration.forms.RegistrationForm及其方法clean_username。

應該很容易將表單擴展爲建議一些用戶名。

這裏是一些示例代碼來生成編號的後綴唯一的用戶名:

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) 

你可以填表選擇領域產生的名稱:該信息真的很感激 http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

+0

嗨maersu,謝謝那。我安裝了該應用程序,並正在處理一些您可能可以幫助我解決的問題。 我堅持延長建議一些用戶名部分。你能幫我什麼,我需要做的 – itsandy 2010-04-29 00:19:47

+0

我已經更新了我的答案。 順便說一句:閱讀我的評論http://stackoverflow.com/questions/2719452/django-forms-help-needed/ – maersu 2010-04-29 09:00:38

+0

您好Maersu,我添加了您提供的示例代碼,以生成帶有數字後綴的用戶名,並將其添加到def clean_username(self):在所有其他驗證下。糾正我,如果我錯了,或者如果它需要去其他地方。 此致敬禮 – itsandy 2010-05-04 01:21:04