Django 重定向 - 不是有效的视图函数或模式名称错误

loc*_*e14 3 python django django-templates django-forms django-views

我是 Django 的新手,正在尝试设置一个简单的联系表单,该表单在成功提交时重定向到感谢页面。我在提交后无法重定向到感谢页面并收到以下错误:

NoReverseMatch at /contact/
Reverse for 'thanks' not found. 'thanks' is not a valid view function or pattern name.
Run Code Online (Sandbox Code Playgroud)

这是我的 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'),
]
Run Code Online (Sandbox Code Playgroud)

和我的 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, ['admin@example.com'])
            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', {})
Run Code Online (Sandbox Code Playgroud)

我在我的 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', # <--
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

有人可以指出我在哪里犯了错误吗?

谢谢!

Rob*_*ond 9

这应该有效

return redirect('home:thanks')
Run Code Online (Sandbox Code Playgroud)