Django在基于类的视图中使用表单方法

时间:2014-05-28 17:24:48

标签: django forms

所以我试图在表单视图中运行一个表单方法。

我在这里看到一个例子: https://docs.djangoproject.com/en/1.6/topics/class-based-views/generic-editing/

以下是我的观点:

class SignupView(generic.FormView):
    template_name = 'signup.html'
    form_class = cp_forms.SignupForm
    success_url = '/control-panel/login/'

    def form_valid(self, form):
        username = form.generate_username()

        return super(SignupView, self).form_valid(form)

以我的形式:

class SignupForm(forms.Form):
     first_name = forms.CharField(max_length=100)
     last_name = forms.CharField(max_length=100)
     birthdate = forms.DateField(widget=forms.TextInput(attrs={'class':'datepicker'}))

    # Generate a custom username using the slug
    def generate_username(self):
        first_name = self.cleaned_data['first_name']
        last_name = self.cleaned_data['last_name']
        birthdate = self.cleaned_data['birthdate']

        member = Member.objects.filter(first_name=first_name).filter(last_name=last_name).filter(birthdate=birthdate)[0]

        return member.slug

    # Generate a random password for the user
    def generate_random_password(self):
        return BaseUserManager.make_random_password()

但它返回错误:

异常类型:AttributeError 例外价值:
' SignupForm'对象没有属性' generate_username'

这是打印类型(表单),目录(表单),表单

的输出

http://gyazo.com/c29832894e9a3255d0d51ce2993af6d5

Check_member_exists,check_member_account_exists,check_member_allowed generate_random_password是我添加的自定义方法...

谢谢, ARA

0 个答案:

没有答案