蟒;Django 在路径名中添加字符导致操作系统错误 22



我一直在尝试让 django 渲染我创建的模板。起初它说模板不存在,但是一旦我修复了错误,它现在正在向路径添加字符并因此找不到模板。

路径应为: C:\Users\ABC\Desktop\science_crowd\Lightweight_Django\placeholder\home.html

但是,该错误指出它找不到: C:\Users\Benjamin\Desktop\science_crowd\Lightweight_Django\placeholder\:\home.html

它无缘无故地添加了一个冒号和另一个反斜杠。

此项目的设置如下:

ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '127.0.0.1').split(',')
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
settings.configure(
    DEBUG = DEBUG,
    SECRET_KEY = SECRET_KEY,
    ALLOWED_HOSTS = ALLOWED_HOSTS,
    ROOT_URLCONF = __name__,
    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ),
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles'
    ),
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': os.path.join(BASE_DIR, 'templates'),
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    "django.contrib.auth.context_processors.auth",
                ],
            },
        },
    ],
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    ),
    STATIC_URL = '/static/',
)

尝试呈现模板的视图:

def index(request):
    example = reverse('placeholder', kwargs = {'width': 50, 'height': 50})
    context = {
        'example': request.build_absolute_uri(example)
    }
    dir = os.path.join(BASE_DIR, 'templates')
    return render(request, 'home.html', context = context)

非常感谢您的帮助!

DIRS设置需要一个列表或元组。尝试:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

最新更新