django 在pythonanywhere上部署时模板不存在错误

bwleehnv  于 2023-08-08  发布在  Go
关注(0)|答案(3)|浏览(129)

我试图在我的域中创建一个博客,使用django 1.9和python 3.5与虚拟环境。在部署Python Anywhere时。我可以渲染HTML。这里是traceback。
第一个月
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'

aemubtdh

aemubtdh1#

如果你有一个名为blog的应用程序(在settings.py中注册),并且你在settings.py中的模板目录下将APP_DIR设置为true,那么很可能你的blog应用程序中有一个名为templates的目录,而在templates中有另一个名为blog的目录。
如果是这样的话,那么blog/templates/blog/post/list.html实际上应该写成:post/list.html前提是您在博客应用程序的博客目录中有一个post目录。

nwsw7zdq

nwsw7zdq2#

你必须使文件夹命名为"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'


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

ppcbkaq5

ppcbkaq53#

您只需重定向到正确的模板目录。我的模板是在文件夹'templates'在根文件夹(同一文件夹包含manage.py).
此设置显示了相同的错误:

'DIRS': ['templates'],

字符串
所以,我把它改成了这个:

'DIRS': ['appointment/templates'],


而且成功了!顺便说一句,“约会”是我项目的名字。
在pythonanywhere中,你应该在/前面加上你的项目名,因为那个文件夹就是你的新家。

相关问题