Django and Custom Form validation

前端 未结 2 693
情深已故
情深已故 2021-02-01 22:53

I am trying to create a custom form field and validate off it. This is version 1.0 of Django.

Here is my form object

class UsernameField(forms.CharField)         


        
2条回答
  •  再見小時候
    2021-02-01 23:18

    That didn't work in 1.0... here is how I solved it

    class UsernameField(forms.CharField):
        def clean(self, request, initial=None):
            field = super(UsernameField, self).clean(request)
    
            from django.contrib.auth.models import User
            user = User(username=request)
            if user is not None:
                raise forms.ValidationError("That username is already taken")
    

提交回复
热议问题