启用以覆盖django-allauth用户注册视图

时间:2014-01-15 21:49:12

标签: python django web-services python-2.7 django-allauth

我编写自定义注册视图的原因是覆盖django-allauth SignupView的form_valid()并按上述方式执行。

由于某些原因,当我点击注册按钮时,控件不会进入CustomSignupView内部,而是转到allauth UserSignup视图的form_valid()并执行该操作。 请帮我看看如何访问CustomSignUpView。 这是我的实现

views.py:

from allauth.account.views import LoginView, SignupView

from allauth.account.forms import SignupForm

from .forms import CustomLoginForm

from .notify import Notification

class CustomSignupView(SignupView):

    notify = Notification()

    def __init__(self, **kwargs):

        super(CustomSignupView, self).__init__(*kwargs)
        self.form_class = SignupForm

    def send_email(self, form): # custom implementation
        self.notify.notify(self.entity_type, form.cleaned_data)


    def form_valid(self, form):

        self.send_email(form)
        return super(CustomSignupView, self).form_valid(form)

这是django的实施:

class SignupView(RedirectAuthenticatedUserMixin, CloseableSignupMixin,
             FormView):
    template_name = "account/signup.html"
    form_class = SignupForm
    redirect_field_name = "next"
    success_url = None

def get_success_url(self):
    # Explicitly passed ?next= URL takes precedence
    ret = (get_next_redirect_url(self.request,
                                 self.redirect_field_name)
           or self.success_url)
    return ret

def form_valid(self, form):
    import pdb; pdb.set_trace()
    user = form.save(self.request)
    return complete_signup(self.request, user,
                           app_settings.EMAIL_VERIFICATION,
                           self.get_success_url())

1 个答案:

答案 0 :(得分:2)

我相信你正在使用基于类的视图,所以你应该实现一个form_valid方法:

class CustomSignupView(SignupView):

    def __init__(self, **kwargs):
        super(CustomSignupView, self).__init__(*kwargs)
        self.form_class = CustomSignupForm

    def send_email(self, form): # custom implementation
        self.notify.notify(self.entity_type, form.cleaned_data)

    def form_invalid(self, form):
        #show some error etc
        pass

    def form_valid(self, form):
        self.send_email(form)
        return super(CustomSignupView, self).form_valid(form)

不知道这是否是你试图实现的目标