[英]DJango Template Not Found Issue
还在学习Django,由于某种原因,我很难掌握一些概念,特别是url / template / view mappings。 尝试将FormView重定向到“完整”页面我有以下内容:
urls.py
from django.urls import path
from .views import *
urlpatterns = [
path('', IndexView.as_view(), name='index'),
path('forgotid/', ForgotId.as_view(),name="forgotid"),
path('forgotid/complete/',ForgotIdComplete.as_view(),name="forgotid_complete"),
path('forgotpwd/', ForgotPwd.as_view(),name="forgotpwd")
]
views.py
from django.shortcuts import render
from django.views.generic import FormView, TemplateView
from .forms import LoginForm, ForgotIDForm
class IndexView(FormView):
template_name = "login/index.html"
form_class = LoginForm
class ForgotId(FormView):
template_name = "login/forgotid.html"
form_class = ForgotIDForm
success_url = 'complete/'
class ForgotIdComplete(TemplateView):
template_name = "login/forgotid/complete/forgotid_complete.html"
def get(self, request):
return render(request, self.template_name, None)
class ForgotPwd(TemplateView):
template_name = "login/forgotpwd.html"
提交ForgotID形式应该我重定向到success_url
,但我得到一个错误,指出模板无法找到。 有人可以解释为什么以及为什么我这样做不正确。
我收到的错误是:
TemplateDoesNotExist at /login/forgotid/complete/
编辑我发现了这个问题。 ForgotIdComplete类中的模板名称应为: login/forgotid_complete
而不是login/forgotid/complete/forgotid_complete.html
把你的success_url改成这个。
from django.core.urlresolvers import reverse_lazy
class ForgotId(FormView):
template_name = "login/forgotid.html"
form_class = ForgotIDForm
success_url = reverse_lazy('forgotid_complete')
此错误意味着,django想要从您提供的路径render
模板,但无法在磁盘上找到它。
转到settings.py
并确保将templates
配置到实际的模板目录。
PROJECT_DIR = os.path.realpath('.')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [(os.path.join(PROJECT_DIR, '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',
],
},
},
]
我发现了这个问题。 ForgotIdComplete
类中的模板名称应为: login/forgotid_complete
而不是login/forgotid/complete/forgotid_complete.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.