我找到了解决方案,请查看下面的答案。这件事发生了 因为我正在探索以前版本的Django的答案。
我是Django的新手,过去一个月我在框架中学到了大部分基础知识。我已将我的应用程序从本地部署到在线服务器。REST API工作正常但现在我无法访问我的前端因为Django在服务器的根目录上,所以在该服务器上结束代码。
服务器和前端代码之间存在一些问题,所以我开发了我的UI,不用担心Django的目录结构(例如静态文件配置等)
现在我想在本地服务器的root上使用Django并访问我的 /front-end/html/index.html 作为主页。
请按以下步骤查看所有代码和目录结构,错误:
我总是遇到以下错误:
TemplateDoesNotExist at /
index.html
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 2.0
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Exception Location: C:\Users\didi\Envs\wantedly_virtual_env\lib\site-packages\django\template\loader.py in select_template, line 47
Python Executable: C:\Users\didi\Envs\wantedly_virtual_env\Scripts\python.exe
Python Version: 3.6.3
我的urls.py文件如下:
urlpatterns = [
path('admin/', admin.site.urls),
url(r"^$", TemplateView.as_view(template_name='index.html')),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
url(r'^rest-auth/login/', include('rest_auth.registration.urls')),
url(r'^refresh-token/', refresh_jwt_token),
url(r'^user/$', DetailsView.as_view(), name='rest_user_details'),
url(r'^', include('api.urls')),
url(r'^api/v1/skills/$', wantedly_app_views.skill_collection),
url(r'^api/v1/skills/(?P<pk>[0-9]+)$', wantedly_app_views.skill_element),
url(r'^api/v1/user/skills/(?P<pk>[0-9]+)$', wantedly_app_views.user_skill_collection),
url(r'^api/v1/user/skill/upvotes/(?P<pk>[0-9]+)$', wantedly_app_views.user_skill_upvotes),
]
我的settings.py包含以下模板结构 - 我无法在此处使用相对路径,因此我正在硬编码完整路径:
TEMPLATE_DIRS = (
('C:/code/django/wantedly/front-end/html').replace('\\','/'),
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
模板加载器postmorterm:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\didi\Envs\wantedly_virtual_env\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\didi\Envs\wantedly_virtual_env\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\didi\Envs\wantedly_virtual_env\lib\site-packages\rest_framework\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\didi\Envs\wantedly_virtual_env\lib\site-packages\allauth\templates\index.html (Source does not exist)
答案 0 :(得分:0)
我找到了解决方案,因为1.8 Django在显式变量TEMPLATE_DIRS中指定了模板元组,而应该是 TEMPLATES 的数组,如下所示:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'C:/code/django/wantedly/front-end/html',
],
'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',
],
},
},
]