【发布时间】:2016-07-03 04:16:33
【问题描述】:
我正在尝试使用 Django 版本 1.9.x 创建我自己的项目,但我无法使/ 的索引页正常工作。
这是我的文件夹结构:
rxe/
urls.py
wsgi.py
settings.py
blog/
migrations/
static/
blog/
css/
js/
img/
templates/
blog/
index.html
admin.py
models.py
tests.py
urls.py
views.py
manage.py
rxe/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'$', include('blog.urls')),
]
blog/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name = 'index'),
]
blog/views.py:
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = 'blog/index.html'
访问localhost:8000/时,报错:
TemplateDoesNotExist at /
博客/index.html
在它的正下方,Template-loader postmortem:
django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/admin/templates/blog/ index.html(源不存在)
django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/auth/templates/blog/ index.html(源不存在)
如果我返回一个HttpResponse('Hello'),它会起作用,就像在教程中一样,但是这个相同的教程没有/ 路径的任何视图,我想要一个在那里。
编辑
rxe/settings.py,它是如何创建的:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
【问题讨论】:
-
@RodXavier
settings.py文件? -
@warath-coder 去哪里?没有正确的地方吗?
-
@RodXavier 我更新了问题。是这样吗?在官方教程中,我没有记录必须修改模板。
-
在 blog/templates/index.html 中,然后在您的代码中将 'index.html' 放入 render 语句中。