我正在尝试使用django 1.9和python 3.5在虚拟环境中创建我的域中的博客。所以在pythonanywhere上部署时。我能够渲染html。这是追溯。
Request Method: GET
Request URL: http://www.example.com/
Django Version: 1.9
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin', `
` 'django.contrib.auth', `
`'django.contrib.contenttypes',`
`'django.contrib.sessions',`
`'django.contrib.messages',`
`'django.contrib.staticfiles',`
`'blog']``
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.app_directories.Loader:
/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/admin/templates/blog/templates/blog/post/list.html(Source does not exist)
* django.template.loaders.app_directories.Loader:
/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/auth/templates/blog/templates/blog/post/list.html(Source does not exist)
* django.template.loaders.app_directories.Loader:
/home/pdlsaroj22/myblog/mysite/blog/templates/blog/templates/blog/post/list.html (Source does not exist)
Using engine django:
* django.template.loaders.app_directories.Loader:
/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/admin/templates/blog/post_list.html (Source does not exist)
* django.template.loaders.app_directories.Loader:
/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/auth/templates/blog/post_list.html (Source does not exist)
* django.template.loaders.app_directories.Loader:
/home/pdlsaroj22/myblog/mysite/blog/templates/blog/post_list.html (Source does not exist)
回溯:
File "/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
response = `self.process_exception_by_middleware(e, request)
File "/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
response = response.render()
File "/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/template/response.py" in render
self.content = self.rendered_content
File "/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/template/response.py" in rendered_content
template = self._resolve_template(self.template_name)
File "/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/template/response.py" in _resolve_template
new_template = self.resolve_template(template)
File "/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/template/response.py" in resolve_template
return select_template(template, using=self.using)
File "/home/pdlsaroj22/.virtualenvs/venv/lib/python3.5/site-packages/django/template/loader.py" in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
Exception Type: TemplateDoesNotExist at /
Exception Value: blog/templates/blog/post/list.html, blog/post_list.html
我的views.py看起来像这样:
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/templates/blog/post/list.html'
答案 0 :(得分:2)
如果您有一个名为blog的应用程序(在settings.py中已注册),并且您在settings.py中的模板目录下将APP_DIR设置为true,则很可能您的博客应用中有一个名为templates的目录,另一个名为blog的模板目录。
如果是这种情况,那么blog/templates/blog/post/list.html
实际上应该写为:post/list.html
,前提是您在博客应用的博客目录中有一个帖子目录。
答案 1 :(得分:1)
您必须将名为"templates"
的文件夹放在settingd.py
中,并将.html文件放在此模板文件夹中。并在以下settings.py
文件中进行更改:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_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',
],
},
},
]
在您的代码中进行如下更改:
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'list.html'
希望这个答案可以帮到你。