So I've been tinkering with upgrading to the new version of Django (1.8). I'm currently on version 1.7 and I am struggling to get my production server to listen to the new settings in 1.8.
As of 1.8, any TEMPLATE_* settings have been deprecated according to the documentation and has been replaced with the TEMPLATES setting.
I'm trying to just continue as I was, but I wish to move to the new settings before the deprecation timeline ends.
In my 1.7 settings I have only got two of the old settings which are now deprecated as follows:
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS += ("django.core.context_processors.request",)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
In the new 1.8 settings I've got the following:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
However when I use these settings, my production server cannot locate the template files, yet my local works just fine.
EDIT: Turns out APP_DIRS setting being missing was playing havoc with openshift. I have all my templates in one directory, not in application dirs, but this seemed to resolve the issue.
It seems that openshift doesn't read the DIRS: setting unless APP_DIRS: is set as True
Doing this, fixed the issue.
Related
Alright, so I'm not that new to programming, but I still have newbie problems.
I'm using Python 2.7 , Django 1.8
I installed django-fobi around 2 days ago and have been pulling my hair since.
Things are better now, but one error refuses to resolve:
TemplateDoesNotExist at 127.0.0.1:8000/fobi/forms/create/ the template is: bootstrap3/create_form_entry.html
although the loader does find it:
project_name/fobi/contrib/themes/bootstrap3/templates/bootstrap3/create_form_entry.html (File exists)
However, the other templates of fobi are working.
To be honest, I'm not sure if I configuredfobi correctly (newbie issues).
Here are my files:
project_name/settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [(os.path.join(SETTINGS_PATH, 'templates'))],
# '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',
"fobi.context_processors.theme", # Important!
"fobi.context_processors.dynamic_values", # Optional
"django.core.context_processors.request"
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'admin_tools.template_loaders.Loader',
],
# 'debug': DEBUG_TEMPLATE,
},
},
]
And I copied the fobi folder from https://github.com/barseghyanartur/django-fobi/tree/master/src and put it at project_name/fobi
Initially, when I did not have the full fobi folder copied, I had just a few templates copied to project_name/templates/bootstrap3/create_form_entry.html, and the template loaded, but then the reversed url fobi_theme.create_form_entry didn't work.
All migrations are applied and the apps are added in INSTALLED_APPS.
Please, help and thank you in advance.
I was recently trying to learn Django for one of my private project.
When came to the Chapter on Template,the Django Book recommended setting template path in settings.py using the the following snippet
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),)
However ,when I opened the file setting.py I found nothing like "TEMPLATE_DIR" but a list:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),],
'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',
],
},
},
]
the the value related to the key "DIR",was a empty list. So I try to filled it with the content shown above.
And then, code something in the views.py(all the import was done )
def current_datetime(request):
now = datetime.datetime.now()
t = get_template("current_datetime.html")
html = t.render(Context({"current_date" : now}))
return HttpResponse(html)
And then mkdir templates in the same folder with setting.py , saved current_datetime.html in folder templates
Finally,run the project.and got the message in my terminal:
WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 > and the TEMPLATES dictionary takes precedence. You must put the values of the > following settings into your default TEMPLATES dict: TEMPLATE_DIRS.
System check identified 1 issue (0 silenced).
June 15, 2017 - 15:32:49
Django version 1.11.2, using settings 'mysite.settings'
Starting development server at 127.0.0.1:8000/
Quit the server with CONTROL-C.
When opened the address (127.0.0.1:8000/time/) in my Safari,here came the
error message:
enter image description here
Anyone help,please ??
There is a warning on TEMPLATE_DIRS, for that just be sure you don't have TEMPLATE_DIRS variable in your settings.py and restart development server.
Then for the error, you are actually using a Context object instead of a dict, you should render the template using a useful shortcut https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#example
from django.shortcuts import render
def current_datetime(request):
now = datetime.datetime.now()
return render(request, "current_datetime.html", {
'current_date' : now,
})
I think you have BASE_DIR in settings.py for define location so use like below
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',
],
},
},
DIRS should be like below
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
I got a templates directory in same level as my project directory so I put in settings.py
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
but Django is not searching templates in apps it stops at DIRS
all apps needed are installed
My solution was to put the app name at the end of INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts' # <= put your app name here i guess?
]
I got this today (I'm on django 2.1). Solution was to add 'myappname' to the INSTALLED_APPS in settings (something you used to have to do on older django versions, but I thought was not mandatory anymore). Without doing this, django template processors were not looking to myappname/templates/myappname/
I also have the TEMPLATES_DIRS set up like some other answers, but for whatever reason that was not sufficient on my current setup (I broke my laptop, so I am django-ing on windows w brand new python 3.7 and brand new django 2.1 (didn't have to do this step the last time I worked w/ django about a week ago...)
Check your DIRS setting. It should include templates directory itself.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
# True or False, depends on your case.
'APP_DIRS': True,
# Default django setup.
'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',
],
},
},
]
Just put the name of the template directory under the TEMPLATES array in the 'DIRS' section in your settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['your folder name'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'logyt_transporte.context_processors.module_variables',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I was attempting to override templates from a third-party package.
My issue was that I had INSTALLED_APPS in the following order:
INSTALLED_APPS = [
# core
# third-party
# mine
]
Because I clearly glossed over the documentation which tells you this is not how it works: https://docs.djangoproject.com/en/3.1/ref/settings/#installed-apps
Specifically the last line:
When several applications provide different versions of the same
resource (template, static file, management command, translation), the
application listed first in INSTALLED_APPS has precedence.
Treat it like fallback order and ensure the overrides are before the originals, et voila. Sigh.
I have the following directory structure in my django 10 project:
/my-project/ # project dir
+app1
+templates
+admin
base.html
404.html
500.html
My templates attribute looks like this in settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'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',
'common.context_processors.site_info',
],
},
},
]
My custom base.html displays on my local machine. When I upload this to my production server it no longer overrides and uses the base.html file in project folder.
I have changed around the order of apps suggested here and tried printing the dirs element of the templates attribute which prints "templates/" like here.
Does anyone know how I can get this to work on my production environment?
You must use absolute path in your settings to avoid issues. For example:
import os
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
# depending where your settings.py live
...
'DIRS': [
os.path.join(PROJECT_ROOT, 'templates'),
],
After upgrade from django 1.7 to 1.8, I can't able to access my admin portal. I get the following error:
DoesNotExist at /admin/login/
Site matching query does not exist.
As mentioned in other questions, I have change my TEMPLATES setting, but it does not help.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'MyANSRSource/templates/MyANSRSource/'),
os.path.join(BASE_DIR, 'employee/template/'),
os.path.join(BASE_DIR, 'employee/emp_photo/'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
#global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
# 'django.template.context_processors.request',
#),
# list if you haven't customized them:
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
If you don't have SITE_ID in your settings, Django 1.8, will try to use the hostname (e.g. 127.0.0.1:8000) to find the site. If you don't have a site with that domain name in your database, then you will get the error:
Site matching query does not exist.
You have a few options:
You might not require the sites framework. You could try removing it from 'django.contrib.sites' from your INSTALLED_APPS setting.
In your settings, set SITE_ID to the value of the site you want to use. SITE_ID = 1 will probably work, if you haven't made any changes to the sites in the database.
Change the domain of the site in the database (e.g. from example.com to 127.0.0.1:8000). This is the most fragile fix, because your project will now work for 127.0.0.1:8000, but not for localhost:8000.
Try to create a new user by running the command manage.py createsuperuser. Log in with this user, you should be able to access the admin panel now.
Then find your user object and make sure it has the correct permissions. as stated in the 1.8 changelog under Minor features.
When I upgraded I lacked the correct permission to access the admin panel.