需要Django表单帮助 [英] Django Forms Help needed

查看:114
本文介绍了需要Django表单帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im new to django,并尝试使用户注册表单,几个验证。
除此之外,我还想要一个用户名建议代码,它将告诉用户他正在尝试注册的用户名是否可用或已经在使用。那么它应该提供几个可能可供选择的建议。任何可能在同一或相同项目上工作的人都可以帮助我。

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 username he is trying to register is available or already in use. Then it should give few suggestions that might be available to choose from. Can anyone who might have worked on the same or somewhat same project help me with this.

谢谢

推荐答案

查看 django-registration 应用程序。并且看看Class registration.forms.RegistrationForm和他们的方法clean_username。

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)

您可以在表单选择字段中填写生成的名称:
http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

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

这篇关于需要Django表单帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆