Skip to content Skip to sidebar Skip to footer

How Can I Redirect To A Thank You For Contacting Us Page After User Submits A Form With Django?

I need to redirect the user to a new page that says, 'Thank you for contacting us' after submitting a form with Django. I have tried using HttpResponseRedirect, reverse but I don't

Solution 1:

Just build another view like the one you already have, with a new template and connect it to a new url. Then redirect the user to that url. Something like this:

in your urls.py:

from django.conf.urls import url
from .views import thank_you
urlpatterns = [
    url(r'^thanks/$', thank_you, name='thank-you'),
]

in your views.py:

from django.shortcuts import render

defthank_you(request):
    template = 'thank_you.html'
    context = {}
    return render(request, template, context)

in your contact view just return a redirect('/thanks/')

Solution 2:

For redirect to thanks page you can use this way

from django.shortcuts import render
from .forms import ContactForm
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect

defcontact(request):
   template = "contact.html"if request.method == "POST":
      form = ContactForm(request.POST)

      if form.is_valid():
         form.save()
         return HttpResponseRedirect(reverse("thanks_view_name"))

   else:
      form = ContactForm()

   context = {
      "form": form,
   }
   return render(request, template, context)

See example https://docs.djangoproject.com/en/2.1/topics/forms/#the-view

Post a Comment for "How Can I Redirect To A Thank You For Contacting Us Page After User Submits A Form With Django?"