HobbyHack
HobbyHack

Reputation: 33

Django CreateView ignoring get_success_url

I have the following view that works except that it is ignoring get_success_url(). It sends the user back to the same url for the form. The user is created. The view class is just an extended createview that I use in many other places without an issue.

class SignUpView(BSModalCreateView):
    model = User
    form_class = SignUpForm
    success_message = 'Please check your email to complete the registration'
    template_name = 'form_modal.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['heading'] = 'Create ID'
        return context

    def form_valid(self, form):
        user = form.save(commit=False)
        user.is_active = False
        user.save()
        # current_site = get_current_site(request)
        current_site = '127.0.0.1:8000'
        mail_subject = 'Activate your account.'
        message = render_to_string('registration/confirm_email.html', {
            'user': user,
            # 'domain': current_site.domain,
            'domain': '127.0.0.1:8000',
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'token': default_token_generator.make_token(user),
        })
        to_email = form.cleaned_data.get('email')
        email = EmailMessage(
            mail_subject, message, to=[to_email]
        )
        email.send()
        return HttpResponseRedirect(self.get_success_url())
    
    def get_success_url(self):
        return reverse('dashboard')

I have tried a bunch of different things t no avail but hope someone else can help.

Upvotes: 0

Views: 226

Answers (1)

HobbyHack
HobbyHack

Reputation: 33

I did some more testing and BSModal seems to be what is stopping this. I am not sure why but it is just easier and cleaner to just send a signal when a user is created. I created a field called is_self_registered on the user model and added the receiver:

@receiver(post_save, sender=User)
def send_activate_email(sender, instance, created, **kwargs):
    user = User.objects.get(pk=instance.id)
    if created and user.is_self_registered:
        print('do email thing')

If one of the admins creates the ID it will be active. If self registration is used the id is not active.

Upvotes: 0

Related Questions