形式上的干净并不意味着

2024-04-26 23:39:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使我的用户的用户名lowercase。但不知怎么的,它不起作用。我只是看不出一个错误。。。为什么不起作用

型号:

class UserProfile(models.Model):
    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User)

    # The additional attributes we wish to include.
    website = models.URLField()

    # Override the __unicode__() method to return out something meaningful!
    def __unicode__(self):
        return self.user.username

形式:

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username', 'email', 'password')

    def clean_username(self):
        username = self.cleaned_data['username'].lower()
        return username

Tags: to用户selfmodelreturnmodelsdefunicode