【问题标题】:Template Does not exist error while deploying on pythonanywhere在 pythonanywhere 上部署时模板不存在错误
【发布时间】:2017-07-18 08:36:37
【问题描述】:

我试图在我的域中使用 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'    

【问题讨论】:

  • 您的模板设置是什么?看起来您已将目录硬编码到本地主目录。
  • 你能给我看看你的 settinsg.py 文件吗
  • 另外,您的 list.html 文件的位置是什么?

标签: python django django-templates pythonanywhere


【解决方案1】:

如果您有一个名为 blog 的应用程序(在您的 settings.py 中注册),并且您在 settings.py 的模板目录下将 APP_DIR 设置为 true,那么您的博客应用程序中可能有一个名为 templates 的目录,并且模板中另一个名为 blog 的目录。

如果是这种情况,那么 blog/templates/blog/post/list.html 实际上应该写成:post/list.html,前提是您在博客应用的博客目录中有一个帖子目录。

【讨论】:

    【解决方案2】:

    你必须在settingd.py 所在的位置创建一个名为"templates" 的文件夹,并将你的.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'
    

    希望这个答案对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 2020-03-07
      • 2018-01-28
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      相关资源
      最近更新 更多