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

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

问题描述

我是 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.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天全站免登陆