简体   繁体   English

Django查找文件路径问题

[英]Django finding the path of the file problem

skin
  -mysite
      -myapp
         -templates
             -index.html
         -mysite
             -urls.py
             -settings.py
         -admin.py
         -views.py

I create the virtual environment in我在中创建虚拟环境

~/home/env 

and the index.html is located in并且 index.html 位于

/home/jake/Gits/skin/mysite/myapp/templates/index.html 


views.py 
from django.shortcuts import render, render_to_response


# Create your views here.
def index(request):
    return render_to_response('index.html')


url.py

from django.conf.urls import include, url
from django.contrib import admin
from myapp import views as v
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', v.index),
]


index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>

and the this Django project did not find the right path而这个 Django 项目没有找到正确的路径

TemplateDoesNotExist at / index.html Request Method: GET Request TemplateDoesNotExist at / index.html 请求方法:GET 请求
URL: http://127.0.0.1:8000/ Django Version: 1.8.1 Exception URL: http : //127.0.0.1 : 8000/ Django 版本:1.8.1 异常
Type: TemplateDoesNotExist Exception Value: index.html Exception类型:TemplateDoesNotExist 异常值:index.html 异常
Location: /home/jake/Gits/skin/mysite/myapp/views.py in index, line 6位置:/home/jake/Gits/skin/mysite/myapp/views.py 在索引中,第 6 行
Python Executable: /home/jake/Gits/skin/bin/python Python Python 可执行文件:/home/jake/Gits/skin/bin/python Python
Version: 3.7.3 Python Path: ['/home/jake/Gits/skin/mysite',版本:3.7.3 Python 路径:['/home/jake/Gits/skin/mysite',
'/home/jake/Gits/skin/lib/python37.zip', '/home/jake/Gits/skin/lib/python37.zip',
'/home/jake/Gits/skin/lib/python3.7', '/home/jake/Gits/skin/lib/python3.7',
'/home/jake/Gits/skin/lib/python3.7/lib-dynload', '/home/jake/Gits/skin/lib/python3.7/lib-dynload',
'/home/jake/anaconda3/lib/python3.7', '/home/jake/anaconda3/lib/python3.7',
'/home/jake/Gits/skin/lib/python3.7/site-packages'] Server time: Wed, '/home/jake/Gits/skin/lib/python3.7/site-packages'] 服务器时间:周三,
8 Jan 2020 06:47:32 +0000 Template-loader postmortem Django tried 2020 年 1 月 8 日 06:47:32 +0000 模板加载器验尸 Django 尝试
loading these templates, in this order:按以下顺序加载这些模板:

Using loader django.template.loaders.filesystem.Loader: Using loader使用加载器 django.template.loaders.filesystem.Loader:使用加载器
django.template.loaders.app_directories.Loader: django.template.loaders.app_directories.Loader:
/home/jake/Gits/skin/lib/python3.7/site-packages/django/contrib/admin/templates/index.html /home/jake/Gits/skin/lib/python3.7/site-packages/django/contrib/admin/templates/index.html
(File does not exist) (文件不存在)
/home/jake/Gits/skin/lib/python3.7/site-packages/django/contrib/auth/templates/index.html /home/jake/Gits/skin/lib/python3.7/site-packages/django/contrib/auth/templates/index.html
(File does not exist) Traceback Switch to copy-and-paste view (文件不存在)回溯切换到复制和粘贴视图
/home/jake/Gits/skin/mysite/myapp/views.py in index索引中的 /home/jake/Gits/skin/mysite/myapp/views.py
return render_to_response('index.html') ... ▶ Local vars return render_to_response('index.html') ... ▶ 局部变量

With minimal change ..以最小的变化..

TEMPLATES = [
    {
        ...., ## Your other settings
        'DIRS': [os.path.join(BASE_DIR, 'templates'),'templates'], ## Little change here
        'APP_DIRS': True,
        ...., ## Your other settings
    },
]

Please let me know if there is still an error.如果还有错误,请告诉我。

In right way your project directory structure should look like this:以正确的方式,您的项目目录结构应如下所示:

YourFolder你的文件夹

-Yourapp -你的应用

-urls.py -urls.py

-settings.py -settings.py

-templates -模板

index.html索引.html

-staticfiles -静态文件
-manage.py -manage.py

It seems that your variable in settings.py(BASE_DIR) - home/You/Gits/skin/mysite/.似乎您在 settings.py(BASE_DIR) 中的变量 - home/You/Gits/skin/mysite/。 There is 2 solution.有2个解决方案。 1.Change your BASE_DIR variable. 1.更改您的 BASE_DIR 变量。 2. change your templates location. 2. 改变你的模板位置。

But my advice, create project properly according to documentation.但我的建议是,根据文档正确创建项目。

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

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