I'm a newbie in Django and am trying to set up a simple contact form which redirects to a thank you page on successful submission. I'm having trouble getting it to redirect to the thanks page after submission and getting the below error:
NoReverseMatch at /contact/
Reverse for 'thanks' not found. 'thanks' is not a valid view function or pattern name.
This is my urls.py
app_name = 'home'
urlpatterns = [
url(r'^$', views.HomePageView.as_view()),
url(r'^contact/$', views.ContactView, name='contact'),
url(r'^thanks/$', views.ThanksView, name='thanks'),
]
and my views.py
def ContactView(request):
if request.method == 'GET':
form = ContactForm()
else:
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
from_email = form.cleaned_data['from_email']
message = form.cleaned_data['message']
try:
send_mail(subject, message, from_email, ['[email protected]'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('thanks')
return render(request, 'contact.html', {'form': form})
def ThanksView(request):
return render(request, 'thanks.html', {})
I added the templates directory in my settings.py
TEMPLATES = [
{
'DIRS': [ os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, '/home', 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
# Third Party Apps
'social_django.context_processors.backends', # <--
'social_django.context_processors.login_redirect', # <--
],
},
},
]
Can someone please point out where I'm making a mistake?
Thanks!
this should work
return redirect('home:thanks')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With