[Answered ]-Where to place logic in a Class Base View?

1👍

I think it should be on post of the CampaignView:

def post(self, request, *args, **kwargs):
   form= self.form_class(request.POST)
   if form.is_valid():
      user_email= form.cleaned_data['email']
      user_of_campaign= Campaign.objects.filter(email= user_email)
      if len(user_of_campaign)>0:
         return redirect('/login', user_of_campaign[0].id)
      else: 
         return redirect('/register')
      return super(CampaignView, self).form_valid(form)
   return super(CampaignView, self).form_invalid(form)
👤ruddra

1👍

You can do this in form_valid itself. Here is an untested code that should help explain the concept:

from django.shortcuts import redirect

def form_valid(self, form):
    email = form.cleaned_data.get("email", "")
    prev_user = Campaign.object.filter(email=email)
    if prev_user:
       return redirect('campaigns_login', self.object.id, prev_user[0].userid)
    else:
       return redirect('campaigns_register', self.object.id)
👤arocks

Leave a comment