Templates Work on Localhost but Not Production / Live Server - python

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.

Related

Creating static files directory

I'm currently using the recent version of django. I seem to be experiencing difficulty in creating a path for my static files directory. The error I get is "TypeError: _getfullpathname: path should be string, bytes or os.PathLike, not list".
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
LOCAL_STATIC_CDN_PATH = [BASE_DIR / 'static_cdn_test']
STATIC_ROOT = '/static_cdn_test/blank/static'
STATICFILES_DIRS = [
BASE_DIR / 'staticfiles',
'/HP/src/staticfiles',
]
MEDIA_ROOT = [BASE_DIR / 'media']
MEDIA_URL = '/media/'
Create a folder and name it "static" in your base directory(where you have your manage.py file) and add the following code to your settings.py file:
> STATIC_URL = '/static/'
>
> STATICFILES_DIRS = [
> STATIC_DIR, ]

How to load static files in python django

I have tried to modify in the settings.py file to allow loading css files but some of html code still doesn't appear with its style, the run in the terminal the command "python manage.py collectstatic" and it says that files are copied but still doesn't appear its effect.
HERE IS THE MODIFIED LINES IN settings.py file
STATIC_ROOT = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
In your settings.py provide:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static_files'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
STATIC_URL = 'http://example.com/static/'
So if static files finders are defined and static_files is declared as source for
static files, whatever you place in
/your_root/static_files/
will come into
/your_root/static
after you run
python manage.py collectstatic
The mistake with
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
is that you're trying to take from and bring to the same directory, which is the destination directory of all static files collected.
It is better to set STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
If you have a folder called static in BASE_DIR, add
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
You may add static_root/ to .gitignore file after you run python manage.py collectstatic
The way I use is:
Create a static/name_of_app directory where I store my static files
2.In Html
first load static files {% load static %}
then link to the path
href = " {% static 'name_of_app/fileName ' %} "
You can add these codes in your settings.py in latest django version
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_ROOT = os.path.join(VENV_PATH, 'media_root')

broken image on my django production site. Why is it?

I just product my site using Heroku and I got a odd problem with my media images broken.
Here is my site structure on Heroku.
-- app
-- manage.py
-- mysite
-- settings
-- __init__.py
-- base.py
-- production.py
-- static
-- media
-- product
-- images
--
-- static_dirs
-- static_root
In my app/mysite/settings/ init.py
from .base import *
try:
from .local import *
live = False
except:
live = True
if live:
from .production import *
and in my base.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'static_dirs'),
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
and in my production.py, I annotated as below.
# Static asset configuration
# import os
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# STATIC_ROOT = 'staticfiles'
# STATIC_URL = '/static/'
# STATICFILES_DIRS = (
# os.path.join(BASE_DIR, 'static'),
# )
Finally, I ran server and I got still broken image shown on my page.
When I look the image carefully through "google inspect element",I can still see the probably right path as below.
<img class="img-responsive" src="/media/products/images/aws.png">
But when I see my /static/media/products/images/ folder, there were images created on development statge only, not the images I just created on production site. (aws.png)
As still beginner for django development, It is a tough to find answer even after few hour's googling.
Please let me sleep & thanks always.
Django is serving static files in debug mode only. The situation and the solutions are explained in the docs: https://docs.djangoproject.com/en/1.4/howto/static-files/
You need to run collectstatic on your production box.

Incorrect TEMPLATE_DIRS location

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'), )

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