2015-06-17 8 views
7

मैं Django के लिए नया हूं और सिर्फ Django आधिकारिक दस्तावेज़ का पालन करता हूं, लेकिन यहां एक समस्या है।django त्रुटि TemplateDoesNotExist

मैं + PyCharm 4.5.1 + अजगर 3.4.3 + विंडोज 8,1

- mysite 
    - main 
     - migrations 
      __init__.py 
     __init__.py 
     admin.py 
     models.py 
     tests.py 
     views.py 
    - mysite 
     __init__.py 
     settings.py 
     urls.py 
     wsgi.py 
    - templates 
     hello.html 
    db.sqlite3 
    manage.py 

इनमें से अधिकांश स्वचालित रूप से बनाई गई हैं

Django 1.8.2 के साथ एक नया Django परियोजना, बनाने के जो मैं संशोधित इस प्रकार हैं:

टेम्पलेट्स/hello.html

<!DOCTYPE html> 
<html> 
<head lang="en"> 
    <meta charset="UTF-8"> 
    <title>Django test</title> 
</head> 
<body> 
hello world! 
</body> 
</html> 

mysite/urls.py

from django.conf.urls import include, url 
from django.contrib import admin 
from main.views import hello 

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^hello/$', hello), 
] 

मुख्य/views.py

from django.shortcuts import render_to_response 

def hello(request): 
    return render_to_response('hello.html', locals()) 

और क्लिक रन और यात्रा स्थानीय होस्ट: 8080, यहाँ हैं परिणाम:

TemplateDoesNotExist at /hello/ 
hello.html 
Request Method: GET 
Request URL: http://127.0.0.1:8000/hello/ 
Django Version: 1.8.2 
Exception Type: TemplateDoesNotExist 
Exception Value:  
hello.html 
Exception Location: C:\Python34\lib\site-packages\django\template\loader.py in get_template, line 46 
Python Executable: C:\Python34\python.exe 
Python Version: 3.4.3 
Python Path:  
['C:\\Users\\kant\\Desktop\\code\\PycharmProjects\\mysite', 
'C:\\Users\\kant\\Desktop\\code\\PycharmProjects\\mysite', 
'C:\\Windows\\SYSTEM32\\python34.zip', 
'C:\\Python34\\DLLs', 
'C:\\Python34\\lib', 
'C:\\Python34', 
'C:\\Python34\\lib\\site-packages'] 

अगर मैं बदल Views.py के रूप में

from django.http import HttpResponse 

def hello(request): 
    return HttpResponse("hello world") 

यह सही ढंग से चलता है।

मैं इसे टेम्पलेट पथ की वजह से हो सकता है लगता है, यहाँ settings.py, संशोधन के बिना स्वचालित रूप से उत्पन्न होता है:

""" 
Django settings for mysite project. 

Generated by 'django-admin startproject' using Django 1.8.2. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.8/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.8/ref/settings/ 
""" 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'p(fs&6e2kg6d3%0txc+9o=(!*8fzt8w5l6neuqer*m9qictsl$' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'main', 
) 

MIDDLEWARE_CLASSES = (
    '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', 
    'django.middleware.security.SecurityMiddleware', 
) 

ROOT_URLCONF = 'mysite.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     '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', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'mysite.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.8/howto/static-files/ 

STATIC_URL = '/static/' 

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'), 
) 

उत्तर

8

आपकी समस्या अपनी सेटिंग्स के साथ है निर्दिष्ट करना होगा। आपके पास वर्तमान में है:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'), 
) 

इस प्रकार आप Django 1.7.x और नीचे टेम्पलेट निर्देशिका सेट अप करते हैं। Django 1.8.x में, बदलने के अपने खाके [] इस तरह पढ़ने के लिए:

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', 
      ], 
     }, 
    }, 
] 

अपने प्रश्न के साथ तो पूरी तरह से किया जा रहा है के लिए धन्यवाद। सौभाग्य!

1

उप-निर्देशिका के अंदर टेम्पलेट डालने का प्रयास करें:

<project>/<app>/templates/<app>/hello.html 

और

return render_to_response('<app>/hello.html', locals()) 

यदि आपके अंदर टेम्पलेट तक पहुंचना चाहते हैं 10 तो आप DIRS अंदर TEMPLATES

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', 
      ], 
     }, 
    }, 
] 
+0

इसकी सेटिंग्स - Django 1.8 टेम्पलेट dirs में टेम्पलेट्स [] के अंदर स्थानांतरित हो गया है। – FlipperPA

संबंधित मुद्दे