PythonAnywhere上的主机上不存在如何解析模板



我做了一个项目,我想在PythonAnywhere上托管它。但如果我运行我的应用程序,我会遇到模板不存在错误。

设置.py

BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-_5&ew#uj!u(kjfmo7b@nxh9=o6fg4!8t3u2a9yj*@vti2u7i^u'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['manojgupta143.pythonanywhere.com']

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Blog',
'taggit',
]

这是我的设置文件,我在其中完成了模板的设置

我的模板文件夹所在的文件夹结构:

/home/manojgupta143/Blog-project-with-django/templates/blog

如果我运行网页,我有这些类型的错误

ImportError at /
Module "django.template.backends.django" does not define a "Django/Templates" attribute/class
Request Method: GET
Request URL:    http://manojgupta143.pythonanywhere.com/
Django Version: 3.2
Exception Type: ImportError
Exception Value:    
Module "django.template.backends.django" does not define a "Django/Templates" attribute/class
Exception Location: /home/manojgupta143/.virtualenvs/myproj/lib/python3.9/site-packages/django/utils/module_loading.py, line 22, in import_string
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.9.5
Python Path:    
['/home/manojgupta143/Blog-project-with-django/',
'/var/www',
'.',
'',
'/var/www',
'/usr/local/lib/python39.zip',
'/usr/local/lib/python3.9',
'/usr/local/lib/python3.9/lib-dynload',
'/home/manojgupta143/.virtualenvs/myproj/lib/python3.9/site-packages']
Server time:    Sat, 05 Feb 2022 07:59:23 +0000

网页配置

Source code:
/home/manojgupta143/Blog-project-with-django/  Go to directory

Working directory:/home/manojgupta143/
WSGI configuration file:/var/www/manojgupta143_pythonanywhere_com_wsgi.py
Python version: 3.9

wsgi.py

# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/manojgupta143/mysite/mysite/settings.py'
## and your manage.py is is at '/home/manojgupta143/mysite/manage.py'
path = '/home/manojgupta143/Blog-project-with-django/'
if path not in sys.path:
sys.path.insert(0, path)
#os.environ.setdefault('DJANGO_SETTINGS_MODULE','/Blogproject.settings')

os.environ['DJANGO_SETTINGS_MODULE'] = 'Blogproject.settings'
#
## then:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

首先,当我尝试运行时,我得到了不存在的错误模板。现在它呈现了一个新的错误。

确保您已经在settings.py 中定义了TEMPLATES url

TEMPLATES = [
{
...
'DIRS': [BASE_DIR/'templates'],
...
]

我也遇到过类似的问题,但我通过更改正确目录中的模板来解决了这个问题。

所以,添加更多的上下文,确保在主目录中添加模板文件夹,而不是像在VS代码中那样添加项目文件夹。

示例 不要这样做

项目名称

  • 模板
  • 项目名称
    • yadayada.html

相反,到此为止。

项目名称

  • 模板
  • yadayada.html

尝试一下,希望这会奏效。

最新更新