Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django redirect - not a valid view function or pattern name error

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!

like image 954
locke14 Avatar asked Sep 16 '25 07:09

locke14


1 Answers

this should work

return redirect('home:thanks')
like image 197
Robin Zigmond Avatar answered Sep 17 '25 19:09

Robin Zigmond