django用户名的不同语言

时间:2014-01-17 05:39:37

标签: python regex django localization chinese-locale

如何使用usercreation表单让用户在用户名字段中使用中文等其他语言?

我尝试过的方法:

  1. 修改用户名字段的正则表达式字段,失败 它告诉我的是:验证不在于字段,而在于用户模型本身

  2. 使用encode(“utf-8”)对输入输入进行编码 它告诉我的是:数据我

  3. 客户端utf转换...

  4. 这是视图中的代码:

    def register_user(request):
        currentPath = request.POST.get('currentPath', '')
        currentPath = currentPath.replace("invalid/", "").replace("registered/", "")
        username=request.POST.get('email')
    password=request.POST.get('password1')
    if request.method == 'POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            user = auth.authenticate(username=username, password=password)
    
            if user is not None:
                auth.login(request, user)
            if "log" in currentPath:
                return HttpResponseRedirect(currentPath)
            else:
                return HttpResponseRedirect('/register_success')
        elif "log" in currentPath:
            return HttpResponseRedirect(currentPath + "registered")
    else:
        form = MyRegistrationForm()
        form.fields['password1'].label = "密码"
        form.fields['password2'].label = "再次输入密码"
    args = {}
    args.update(csrf(request))
    
    args['form'] = form
    
    return render_to_response('register.html', args)
    

    我认为form.save()是问题的来源......验证器会自动验证字段,无论什么和中文字符都无法通过验证。

    以下是自定义注册表单,我尝试使用用户名字段覆盖正则表达式字段,但它不起作用。

    my_default_errors = {
        'required': '这个格子是必填的',
        'invalid': '请输入符合要求的值',
    }
    class MyRegistrationForm(UserCreationForm):
        error_messages = {
            'duplicate_username': ("同样的用户名已经被注册了"),
            'password_mismatch': ("和上次输入的不一样,请重新输入!"),
        }
    # this variable defines a field in the customized form, not a model datafield
    username = forms.RegexField(label = "用户名(其他所有人可见,请使用英文)", required=True, max_length=30,
        regex=r'^[\w.@+-\/\\]+$',
        error_messages=my_default_errors,)
    email = forms.EmailField(label="邮箱(用于登录)" ,required=True, max_length = 75, error_messages = my_default_errors, )
    password1 = forms.CharField(label= "密码", required=True, widget=forms.PasswordInput, error_messages = my_default_errors,)
    password2 = forms.CharField(label= "请再次输入", required=True, widget=forms.PasswordInput, error_messages = my_default_errors,)
    
    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')
    
    def clean_email(self):
        email = self.cleaned_data["email"]
        try:
            user = User.objects.get(email=email)
            raise forms.ValidationError("这个邮箱地址已被注册,是否忘记了密码?")
        except User.DoesNotExist:
            return email
    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        user.email = self.cleaned_data["email"]
        user.is_active = True # change to false if using email activation
        if commit:
            user.save()   
        return user
    

0 个答案:

没有答案