简体   繁体   English

DJango模板未找到问题

[英]DJango Template Not Found Issue

Still learning Django and for some reason I have difficulty grasping some concepts especially the url / template / view mappings. 还在学习Django,由于某种原因,我很难掌握一些概念,特别是url / template / view mappings。 Attempting to reirect a FormView to a "Complete" page I have the following: 尝试将FormView重定向到“完整”页面我有以下内容:

urls.py 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 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"

submitting the ForgotID form should redirect me to the success_url but I get an error stating that the template could not be found. 提交ForgotID形式应该我重定向到success_url ,但我得到一个错误,指出模板无法找到。 Can someone please explain what and why I am doing this incorrectly. 有人可以解释为什么以及为什么我这样做不正确。

The error I am receiving is: 我收到的错误是:

TemplateDoesNotExist at /login/forgotid/complete/

My folder structure: 我的文件夹结构: 在此输入图像描述

EDIT I found the issue. 编辑我发现了这个问题。 The template name in the ForgotIdComplete class should read: login/forgotid_complete and not login/forgotid/complete/forgotid_complete.html ForgotIdComplete类中的模板名称应为: login/forgotid_complete而不是login/forgotid/complete/forgotid_complete.html

change your success_url to this. 把你的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')

This error means that, django wanted to render the template from the path you provided but couldn't find it on the disk. 此错误意味着,django想要从您提供的路径render模板,但无法在磁盘上找到它。

Go to settings.py and make sure that templates is configured to your actual templates directory. 转到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',
            ],
        },
    },
]

I found the issue. 我发现了这个问题。 The template name in the ForgotIdComplete class should read: login/forgotid_complete and not login/forgotid/complete/forgotid_complete.html ForgotIdComplete类中的模板名称应为: login/forgotid_complete而不是login/forgotid/complete/forgotid_complete.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM