Question

I'm currently implementing a pretty standard class-based FormView and on form_valid I want to redirect to another view (not class-based). However, the URL redirect happens ok, but the template shown still reflects the form view from before.

class EmailView(FormView):
    template_name = 'landing/index.html'
    form_class = EmailForm
    success_url = 'success'

    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            address = form.cleaned_data['email']
            e = Email(email_address=address.lower())

            return redirect('success')

        return render(request, self.template_name, {'form': form})

def success(request):
    return render(request, 'landing/thank-you.html')

And yes, I've correctly mapped 'success' to my success view in URLConf. Any ideas?

Was it helpful?

Solution

What you have is a URL that matches everything and is matching before your thank-you page.

You need to add a $ after your ^ to make sure that URL only matches exactly one pattern, and not basically everything.

url(r'^$', views.EmailView.as_view(), name='landing'),
#      ^ this must be added
url(r'^thank-you/$', views.success, name='success') #......
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top