Incorrect TEMPLATE_DIRS location - python

I'm trying to deploy my Django application at webfaction but the path to TEMPLATE_DIRS in settings seems to be incorrect.
My project is located under webapps/django/edmhunters
My settings.py file is under webapps/django/edmhunters/edmhunters
My template folder is at webapps/django/edmhunters/templates
In my settings.py file this is what i'm doing
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
But I still get TemplateDoesNotExist error. What would be the correct path?

Your templates directory is one more level up, you can do as below to get parent directory and use it to get templates directory.
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.normpath(os.path.dirname(SETTINGS_PATH))
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)

TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, '../templates'), )
use this instead of
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'), )

Related

Templates Work on Localhost but Not Production / Live Server

I have a setup to recognize the static files and template files at the top level (above the apps folders) like so:
import os
BASE_DIR = os.path.realpath(os.path.dirname(__file__))
...
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
TEMPLATE_DIRS = (
BASE_DIR + '/templates/',
)
It works fine in the local environment, but online I get this:
TemplateDoesNotExist at /
home.html
What could be causing this inconsistency?
Try this...
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
If it doesn't work, declare your template path in a variable like
template_path = /path/to/template
TEMPLATE_PATH_DIRS = (
template_path + '/templates',
)
TEMPLATE_DIRS = TEMPLATE_PATH_DIRS
It will work, try it.

Django 1.8: Help making filenames RELATIVE

I'm new to Django. First I'll explain my issue and my logic. I want to make my filepaths RELATIVE as opposed to the ABSOLUTE they are now so I can work on my laptop and PC and have everything show up as is.
I know I'll have to alter MEDIA_URL in the settings.py below. Does
MEDIA_URL = os.path.join(BASE_DIR, 'articles/static/articles/media/')
STATIC_URL = os.path.join(BASE_DIR, 'articles/static/')
make sense? I mean logically to me its saying from BASE_DIR which prints to C:\Users\kevIN3D\Documents\GitHub\articleTestProject\articleTestSite, would step into articles/static/articles/media/ or does the fact that
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
is abspath(...) change everything?
settings.py
"""
Django settings for articleTestSite project.
Generated by 'django-admin startproject' using Django 1.8.
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.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
MEDIA_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/static/articles/'
MEDIA_URL = '/media/'
# 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 = '2u#9=qkari39(465g+u!2t7*9tt_pdv)%155jdgxnki5#jujje'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'articles',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
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 = 'articleTestSite.urls'
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',
],
},
},
]
WSGI_APPLICATION = 'articleTestSite.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-5'
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_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/'
STATIC_URL = '/static/'
urls.py
urlpatterns = [
url(r'^articles/', include('articles.urls')),
url(r'^$', HomePage.as_view(), name='home'),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_ROOT)
My issue is when I try to use '/' in my declaration it prints out the filename as 'C:\Users\kevIN3D\Documents\GitHub\articleTestProject\articleTestSite\articles/static/' so it uses incorrect slashes for myself.
How do I go about making my page have relative filenames? I want to be able to work on this between my laptop and my PC, but as it currently stands I can only work on it on my PC because all the filenames are absolute and just give me broken links on my laptop.
Here is a link to the repository Github Repository. Please any help would be appreciated, I'm completely stumped. If someone could get that working, with images and custom CSS and then kind of walk me through what you did. Its my first time trying to distribute a Django file to more then just the local host.
Let me explain settings:
Right wave to separate the path
Pass every folder to the os.path.join() method. Example:
os.path.join(BASE_DIR, 'articles', 'static')
STATIC_URL, MEDIA_URL
This variables are passed to your template context proccessor (if you use django.template.context_processors.media) without any changes. This variables are used to make client-side links to your static and media content.
You should set your STATIC_URL and MEDIA_URL manually. Like that:
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
Then you can use them in templates:
<img src="{{STATIC_URL}}img/logo.png">(...)
STATIC_ROOT, MEDIA_ROOT, STATICFILES_DIRS
STATIC_ROOT is used to bring all your static files in one place by collectstatic command (if you're using django simple server, you shouldn't care about it).
STATICFILES_DIRS is the folder when your django simple server (manage.py runserver command) gets files to serve. If you will run collectstatic command, evety file from every dir listed in STATICFILES_DIRS copied be moved in STATIC_ROOT.
MEDIA_ROOT is the folder where Django will hold user-uploaded files (ImageField, FileField)
You should use absolute path in STATIC_ROOT, MEDIA_ROOT, STATICFILES_DIRS:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'example', 'static', 'folder',)
Please note. If you have static folder in your app folder and your app is listed in INSTALLED_APPS this folder will be added to your STATICFILES_DIRS automatically.
Serving static and media files during development
It's really simple just add this lines after your urlpatterns:
urlpatterns = patterns('',
... your patterns...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And don't forget to include settings:
from django.conf import settings
Serving on production server
Run collectstatic command to bring all your static content in one place.
Server your static and media folder using your apache/nginx/etc server:
Alias /media/ /path/to/your/media
Alias /static/ /path/to/your/static/
So I think I figured it out,
I was looking at things far to high-level so to speak. When in reality the answer was just simple. After giving myself a
print(BASE_DIR)
and having it spit out C:\Users\kevIN3D\Documents\GitHub\articlesTestProject\articleTestSite I realized all I needed to do was append that to my different ROOT variables.
MEDIA_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/static/articles/'
was my old MEDIA_ROOT, this was clearly absolute. BUT from this I can see that it shares that same filepath as BASE_DIR, so I went from there and
MEDIA_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/static/articles/'
became
MEDIA_ROOT = os.path.join(BASE_DIR, 'articles/static/articles/').replace('\\', '/') #run replace to convert UNIX slashes on Windows slashes
I had to do the same thing for STATIC_ROOT
STATIC_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/'
became
STATIC_ROOT = os.path.join(BASE_DIR, 'articles')

Template error in django for openshift

I am trying to set up a Django 1.7 project to push to openshift. I'm following https://github.com/jfmatth/openshift-django17. I've sucessfully got the initial project going on openshift, now I'm trying to move a local project into this file structure so I can deploy it.
When I run the project I get:
TemplateDoesNotExist at /index/
The templates are all in the static/templates folder (in the screenshot). My settings url contains:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
......
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'wsgi','static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
How can I get help django find the templates?
move out your templates from /static/ in the parent directory, you don't want them moved to wsgi/static when doing collectstatic, then add this setting:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)

static file not served in heroku app

I deployed my final working project to heroku.
The problem is only html content loading, Static files are missing. I tried lots but missing something. Can you spot the error.
# .. settings.py
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)
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.
os.path.join(BASE_DIR, 'static/'),
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
You must give the STATIC_ROOT ...(It can not be empty). Because the "HEROKU", always run 'collectstatic' while deploying.. and it creates the folder like as 'static files' and all the process running by the way of this folder contents.

I can't define STATICFILES_DIRS using Python to figure out the path

I want to be able to define my settings for static/media files using python to get the paths so I don't need different settings on my dev machine and my server.
So I have these settings;
import os
from unipath import Path
### PATH CONFIGURATION
# Absolute filesystem path to the top-level project folder
SITE_ROOT = Path(__file__).ancestor(3)
### MEDIA CONFIGURATION
MEDIA_ROOT = SITE_ROOT.child('media')
MEDIA_URL = '/media/'
### END MEDIA CONFIGURATION
### STATIC CONFIGURATION
STATIC_ROOT = SITE_ROOT.child('static')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
My problem is that locally it won't load the static files and the terminal says that STATICFILES_DIRS should not contain the STATICFILES_ROOT.
Is it possible to get Python to load the paths like this or am I wasting my time?
There's nothing wrong with your code per se, it's just that the point of the staticfiles app is to copy the files from the directories specified in STATICFILES_DIRS into the directory specified in STATIC_ROOT, so it doesn't make much sense to include the STATIC_ROOT directory in the STATICFILES_DIRS setting.
Unless you're actually using the staticfiles app with ./manage.py collectstatic, you may as well just leave the STATICFILES_DIRS setting empty, i.e. just change...
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
...to...
STATICFILES_DIRS = ()
Do like this:
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static/'),
)
That should work. Hope it helps!
+1 for both other answers. If you get tired of typing os.path.bla a lot here's a shortcut you can position at the top of your settings file (or import from anywhere else)
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
STATICFILES_DIRS = (
rel('static'),
)

Categories

Resources