如何向classview添加逻辑

时间:2015-12-11 20:00:14

标签: python django

我有以下课堂观点:

class RegisterView(generic.CreateView):
    model = User
    form_class = forms.RegisterForm
    template_name = 'register.html'
    success_url = reverse_lazy('platforms')

    def form_valid(self, form):
        response = super(RegisterView, self).form_valid(form)
        user = authenticate(username=form.instance.username, password=form.cleaned_data['password'])
        login(self.request, user)
        return response

我如何在上面的语句中添加以下语句,我会在正常函数中做什么?

def normal_register_function(request):
    if request.user is not None:
        return redirect('home')
    # register logic
    return render(request, 'register.html', data)

2 个答案:

答案 0 :(得分:3)

像这样:

class RegisterView(generic.CreateView):
    model = User
    form_class = forms.RegisterForm
    template_name = 'register.html'
    success_url = reverse_lazy('platforms')

    def get(self, request, *args, **kwargs):
        if request.user is not None:
            return redirect('home')
        # register logic
        return render(request, 'register.html', data)

    def form_valid(self, form):
        response = super(RegisterView, self).form_valid(form)
        user = authenticate(username=form.instance.username, password=form.cleaned_data['password'])
        login(self.request, user)
        return response

答案 1 :(得分:0)

您可以使用http://ccbv.co.uk/projects/Django/1.4/中描述的调度方法:

var results = db.Fetch<Person>(sql, new {t1 = t1, t2 = t2});