【问题标题】:TemplateDoesNotExist at /. i dont know where i'm wrong [duplicate]TemplateDoesNotExist 在 /。我不知道我错在哪里[重复]
【发布时间】:2023-03-30 03:10:02
【问题描述】:

我不知道我错在哪里,我对此很陌生,所以请告诉我我错在哪里。 请帮帮我,这是 视图.py

from django.shortcuts import render


def home(request):
    return render(request, 'home.html')

urls.py

"""portfolio URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home)
]

路径

manage.py
db.sqlite3
    views.py
    urls.py
    settings.py
    asgi.py
    wsgi.py
    __init__.py
    templates/home.html

我正在使用 Django 3.0.10

【问题讨论】:

标签: python django


【解决方案1】:

你的路错了。

您的路径当前似乎是appName > templates > home.html

必须是appName > templates > appName > home.html

因此,基本上,在模板目录中添加一个新目录,并将新目录命名为与您为应用命名的任何名称相同,然后将您的 html 文件保存在其中。

编辑:您似乎没有应用程序。

您应该创建一个应用程序... cd 到您的项目目录并运行 django startapp appName

将其添加到您的 settings.py 配置中

INSTALLED_APPS = [
'appname.apps.appNameConfig'
]

然后在该应用程序目录中执行上述操作...appName > templates > appName > home.html

【讨论】:

  • 他甚至没有应用程序......
  • @BriseBalloches 谢谢,你是对的。已编辑。
【解决方案2】:

您不是创建了一个用于管理 views.py 的应用程序吗?我无法从您的代码中获取信息。我认为问题是针对 URL 路径造成的。 尝试在根文件夹中创建应用程序,创建视图,添加应用程序的 url.py,最后添加根 URL 模式。更多详情请查看:Django Documantation

【讨论】:

    【解决方案3】:

    你有两个选择:

    • BASE_DIR/templates(选项 1)
    • BASE_DIR/APP_DIR/templates/appname(选项 2)

    如果您使用的是option 1,则必须将其添加到您的TEMPLATES 设置变量中(因为django 不会查找它)

    TEMPATES = [
        ...
        DIRS: [
            # django >= 3
            BASE_DIR / 'templates',
    
            # django < 3
            os.path.join(BASE_DIR, 'templates') # add 'import os' to your settings.py
        ]
    ]
    

    那么在你看来:

    def view(request):
        # Case 1:
        # assuming your html file `file.html` exists here `BASE_DIR/templates/myapp/file.html`
        # use this
        # return render(request, 'myapp/file.html')
    
        # Case 2:
        # assuming your html file `file.html` exists here `BASE_DIR/templates/file.html`
        # use this
        # return render(request, 'file.html')
    
        # Case 3:
        # assuming your html file `file.html` exists here `BASE_DIR/APP_DIR/templates/myapp/file.html`
        # use this
        # return render(request, 'myapp/file.html')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多