2017-06-12 16 views
0

我一直在试图让django呈现我创建的模板。起初它说模板不存在,但是一旦我修复了这个错误,它现在将字符添加到路径中,并且因为这个原因而没有找到模板。Python; Django将字符添加到导致操作系统错误的路径名称22

的路径应该是: 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) 

非常感谢你的帮助!

回答

2

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

'DIRS': [os.path.join(BASE_DIR, 'templates')], 
相关问题