Не получатся указать путь к шаблонам - 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
Листинг программы
  1. from django.shortcuts import render
  2. from django.http.response import HttpResponse
  3. from django.template.loader import get_template
  4. from django.template import Context
  5. from django.shortcuts import render_to_response
  6. from article.models import Article
  7.  
  8. # Create your views here.
  9. def two (request):
  10. view = "template_three"
  11. html = "<html><body> this is %s view</body></html>" % view
  12. return HttpResponse(html)
  13.  
  14. def one (request):
  15. view = "template_three"
  16. t = get_template('myview.html')
  17. html = t.render(Context({'name': view}))
  18. return HttpResponse(html)
  19.  
  20. def template_three_simple(request):
  21. view = "template_three"
  22. return render_to_response ('myview.html',{'name': view})
  23. def articles(request):
  24. return render_to_response ('articles.html', {'articles': Article.objects.all()})
  25. def article(request, article_id=1):
  26. return render_to_response ('article.html', {'article': Article.objects.get(id=article_id)})
setings.py
Листинг программы
  1. """
  2. Django settings for firstapp project.
  3. Generated by 'django-admin startproject' using Django 1.9.
  4. For more information on this file, see
  5. [url]https://docs.djangoproject.com/en/dev/topics/settings/[/url]
  6. For the full list of settings and their values, see
  7. [url]https://docs.djangoproject.com/en/dev/ref/settings/[/url]
  8. """
  9. import os
  10. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  11. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  12.  
  13. # Quick-start development settings - unsuitable for production
  14. # See [url]https://docs.djangoproject.com/en/dev/howto/deployment/checklist/[/url]
  15. # SECURITY WARNING: keep the secret key used in production secret!
  16. SECRET_KEY = 'aa_(9)^jv+6+fczpfs328$^bve9^r(g&r#pirjg2idgse&(a%_'
  17. # SECURITY WARNING: don't run with debug turned on in production!
  18. DEBUG = True
  19. ALLOWED_HOSTS = []
  20. [B]
  21. TEMPLATE_DIRS = (
  22. 'C:/django_code/firstapp/firstapp/templates',
  23. )[/B]
  24.  
  25. # Application definition
  26. INSTALLED_APPS = [
  27. 'django.contrib.admin',
  28. 'django.contrib.auth',
  29. 'django.contrib.contenttypes',
  30. 'django.contrib.sessions',
  31. 'django.contrib.messages',
  32. 'django.contrib.staticfiles',
  33. 'article',
  34. ]
  35. MIDDLEWARE_CLASSES = [
  36. 'django.contrib.sessions.middleware.SessionMiddleware',
  37. 'django.middleware.common.CommonMiddleware',
  38. 'django.middleware.csrf.CsrfViewMiddleware',
  39. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  40. 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  41. 'django.contrib.messages.middleware.MessageMiddleware',
  42. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  43. 'django.middleware.security.SecurityMiddleware',
  44. ]
  45. ROOT_URLCONF = 'firstapp.urls'
  46. TEMPLATES = [
  47. {
  48. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  49. 'DIRS': [],
  50. 'APP_DIRS': True,
  51. 'OPTIONS': {
  52. 'context_processors': [
  53. 'django.template.context_processors.debug',
  54. 'django.template.context_processors.request',
  55. 'django.contrib.auth.context_processors.auth',
  56. 'django.contrib.messages.context_processors.messages',
  57. ],
  58. },
  59. },
  60. ]
  61. WSGI_APPLICATION = 'firstapp.wsgi.application'
  62.  
  63. # Database
  64. # [url]https://docs.djangoproject.com/en/dev/ref/settings/#databases[/url]
  65. DATABASES = {
  66. 'default': {
  67. 'ENGINE': 'django.db.backends.sqlite3',
  68. 'NAME': os.path.join(BASE_DIR, 'our_db.sqlite3'),
  69. }
  70. }
  71.  
  72. # Internationalization
  73. # [url]https://docs.djangoproject.com/en/dev/topics/i18n/[/url]
  74. LANGUAGE_CODE = 'ru-Ru'
  75. TIME_ZONE = 'UTC'
  76. USE_I18N = True
  77. USE_L10N = True
  78. USE_TZ = True
  79.  
  80. # Static files (CSS, JavaScript, Images)
  81. # [url]https://docs.djangoproject.com/en/dev/howto/static-files/[/url]
  82. STATIC_URL = '/static/'
  83. STATICFILES_DIRS = (
  84. # Put strings here, like "/home/html/static" or "C:/www/django/static".
  85. # Always use forward slashes, even on Windows.
  86. # Don't forget to use absolute paths, not relative paths.
  87. 'C:/Python27/Scripts/tests/static', # тут путь к папке, в котором лежат css файлы
  88. )
При попытке открыть это 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\ все работает. Не получается переназначит папку шаблонов, что не так?

Решение задачи: «Не получатся указать путь к шаблонам»

textual
Листинг программы
  1. TEMPLATES = [
  2.     {
  3.         'BACKEND': 'django.template.backends.django.DjangoTemplates',
  4.         'DIRS': ['templates', 'C:/django_code/firstapp/firstapp/templates'],

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 3.857 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут