【问题标题】:Django redirect - not a valid view function or pattern name errorDjango 重定向 - 不是有效的视图函数或模式名称错误
【发布时间】:2019-02-15 09:28:51
【问题描述】:

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

NoReverseMatch at /contact/
Reverse for 'thanks' not found. 'thanks' is not a valid view function or pattern name.

这是我的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'),
]

还有我的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', {})

我在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', # <--
            ],
        },
    },
]

谁能指出我哪里出错了?

谢谢!

【问题讨论】:

    标签: python django django-forms django-templates django-views


    【解决方案1】:

    这应该可以工作

    return redirect('home:thanks')
    

    【讨论】:

    • 您好,感谢您的回复。但即使使用reverse('thanks'),我仍然会遇到同样的错误。有趣的是,即使没有reverse,在不同的应用程序中重定向到另一个视图名称也有效。这让我觉得不知何故,home 应用程序中的视图 / url 名称似乎无法识别..
    • @locke14 - 抱歉,似乎没有必要反过来(但我一直使用它,我只能看到这是错误的)。我有一个新建议(如果可行,我将编辑上面的答案)-因为我看到您已将 app_name 设置为 'home',请尝试执行 return redirect('home:thanks')
    • return redirect('home:thanks')works !!感谢您的帮助。
    • 请注意,这些名称是在各自的urls.py 文件中定义的,并且路径由它们的“名称”引用,这些名称通常与实际路径名称不同!
    【解决方案2】:

    希望对你有帮助:

    return redirect(reverse('appname:urlname'))
    

    或者使用 kwargs,如果是 'urlname/':

    return redirect(reverse('appname:urlname', kwargs={'kwarg':value}))
    

    【讨论】:

      【解决方案3】:

      如果你定义 app_name 为 app 的名字 你应该这样写你的视图的url

      <a href='{% url "name_of_app:name_of_view" %}'> Link </a>
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-25
        • 2019-06-27
        • 2018-11-29
        • 2020-03-13
        • 2019-04-21
        • 2021-07-18
        • 2019-04-06
        • 2021-04-28
        相关资源
        最近更新 更多