Не получатся указать путь к шаблонам - Python
Формулировка задачи:
Не получается указать путь к файлу html
Прописываю в setins.py
TEMPLATE_DIRS = (
'C:/django_code/firstapp/firstapp/templates',
)
не получается
Сервер пытается найти файл в C:\Python34\lib\site-packages\django-1.9-py3.4.egg\django\contrib\admin\templates\myview.html (Source does not exist)
Почему он ищет его там? я ведь переназначаю директорию.
vievs.py
setings.py
При попытке открыть это html на локальном сервере http://127.0.0.1:8000/q/3/
Выдаёт ошибку что не может найти myview.html по пути django.template.loaders.app_directories.Loader: C:\Python34\lib\site-packages\django-1.9-py3.4.egg\django\contrib\admin\templates\myview.html (Source does not exist)
Кода я кладу файл myview.html в директорию C:\Python34\lib\site-packages\django-1.9-py3.4.egg\django\contrib\admin\templates\ все работает.
Не получается переназначит папку шаблонов, что не так?
Листинг программы
- from django.shortcuts import render
- from django.http.response import HttpResponse
- from django.template.loader import get_template
- from django.template import Context
- from django.shortcuts import render_to_response
- from article.models import Article
- # Create your views here.
- def two (request):
- view = "template_three"
- html = "<html><body> this is %s view</body></html>" % view
- return HttpResponse(html)
- def one (request):
- view = "template_three"
- t = get_template('myview.html')
- html = t.render(Context({'name': view}))
- return HttpResponse(html)
- def template_three_simple(request):
- view = "template_three"
- return render_to_response ('myview.html',{'name': view})
- def articles(request):
- return render_to_response ('articles.html', {'articles': Article.objects.all()})
- def article(request, article_id=1):
- return render_to_response ('article.html', {'article': Article.objects.get(id=article_id)})
Листинг программы
- """
- Django settings for firstapp project.
- Generated by 'django-admin startproject' using Django 1.9.
- For more information on this file, see
- [url]https://docs.djangoproject.com/en/dev/topics/settings/[/url]
- For the full list of settings and their values, see
- [url]https://docs.djangoproject.com/en/dev/ref/settings/[/url]
- """
- import os
- # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
- # Quick-start development settings - unsuitable for production
- # See [url]https://docs.djangoproject.com/en/dev/howto/deployment/checklist/[/url]
- # SECURITY WARNING: keep the secret key used in production secret!
- SECRET_KEY = 'aa_(9)^jv+6+fczpfs328$^bve9^r(g&r#pirjg2idgse&(a%_'
- # SECURITY WARNING: don't run with debug turned on in production!
- DEBUG = True
- ALLOWED_HOSTS = []
- [B]
- TEMPLATE_DIRS = (
- 'C:/django_code/firstapp/firstapp/templates',
- )[/B]
- # Application definition
- INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- 'article',
- ]
- 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 = 'firstapp.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 = 'firstapp.wsgi.application'
- # Database
- # [url]https://docs.djangoproject.com/en/dev/ref/settings/#databases[/url]
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': os.path.join(BASE_DIR, 'our_db.sqlite3'),
- }
- }
- # Internationalization
- # [url]https://docs.djangoproject.com/en/dev/topics/i18n/[/url]
- LANGUAGE_CODE = 'ru-Ru'
- TIME_ZONE = 'UTC'
- USE_I18N = True
- USE_L10N = True
- USE_TZ = True
- # Static files (CSS, JavaScript, Images)
- # [url]https://docs.djangoproject.com/en/dev/howto/static-files/[/url]
- STATIC_URL = '/static/'
- STATICFILES_DIRS = (
- # Put strings here, like "/home/html/static" or "C:/www/django/static".
- # Always use forward slashes, even on Windows.
- # Don't forget to use absolute paths, not relative paths.
- 'C:/Python27/Scripts/tests/static', # тут путь к папке, в котором лежат css файлы
- )
Решение задачи: «Не получатся указать путь к шаблонам»
textual
Листинг программы
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': ['templates', 'C:/django_code/firstapp/firstapp/templates'],
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д