django-pipeline js compressor is not working - python

This is my settings.py looks like :
kProjectRoot = abspath(normpath(join(dirname(__file__), '..')))
MEDIA_ROOT = os.path.join(kProjectRoot, 'abc/media/')
MEDIA_URL = '/media/'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'south',
'xlrd',
'pipeline',
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_YUI_BINARY='C:/Python27/Lib/site-packages/yuicompressor-2.4.8-py2.7.egg/yuicompressor'
PIPELINE_JS = {
'site': {
'source_filenames': (
'media/js/zk.base.js',
'media/js/zk.popupmenu.js',
'media/js/zk.tree.js',
'media/js/zk.treenode.js',
),
'output_filename': 'media/js/script.min.js',
}
}
What is the mistake i am doing , please guide me . I think that it should create a script.min.js in my media/js/ , which i can load in templates.

Did you run ./manage.py collectstatic --noinput
If that doesn't work, make sure these are in your settings.py and run collectstatic again, your assets will be under the build folder.
PIPELINE_ENABLED = True
STATICFILES_FINDERS = (
'pipeline.finders.FileSystemFinder',
'pipeline.finders.AppDirectoriesFinder',
'pipeline.finders.PipelineFinder',
'pipeline.finders.CachedFileFinder',
)
STATIC_ROOT = normpath(join(SITE_ROOT, 'build'))
STATIC_URL = '/assets/'
STATICFILES_DIRS
STATICFILES_DIRS = (
normpath(join(SITE_ROOT, 'static')),
)
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
)
Then you can use your assets by putting the following in your template:
{% load compressed %}
{% compressed_js 'site' %}
Hope it helps.

Replace back-slashes \ with forward-slashes / in the path of PIPELINE_YUI_BINARY.

Related

Server Error After Setting DEBUG=False Django

I keep getting Sever error- 500 after setting DEBUG=True both on localhost and the hosted version and I don't know the cause of the error.
Here are some my code in my settings.py file
import os
from pathlib import Path
from decouple import config
import django_heroku
import dj_database_url
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = str(os.getenv('SECRET_KEY'))
DEBUG = config("DEBUG", default=False, cast=bool)
ALLOWED_HOSTS = ["127.0.0.1", "localhost", "cv-build.onrender.com"]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"cloudinary_storage",
"cloudinary",
'app',
]
MEDIA_URL = "/cv-build/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATIC_URL = "/static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'staticfiles')]
STATIC_ROOT = os.path.join(BASE_DIR, "app/static")
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
DEFAULT_FILE_STORAGE = "cloudinary_storage.storage.MediaCloudinaryStorage"
CLOUDINARY_STORAGE = {
"CLOUD_NAME": config("CLOUDINARY_CLOUD_NAME"),
"API_KEY": config("CLOUDINARY_API_KEY"),
"API_SECRET": config("CLOUDINARY_API_SECRET"),
}
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
I've tried to sort it out by checking the Allowed Host, static settings and all but it is the same result.
Thanks.

Django - Not Found The requested resource was not found on this server on Production

I was trying to deploy my Django project on Render . Everything works fine except for the media files. I can't figure out the issue here.
I have added the followings into my settings.py:
DEBUG=False
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / "staticfiles"
STATICFILES_DIRS = [
BASE_DIR / "static"
]
MEDIA_URL = '/contents/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/contents/')
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
urls.py
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
I have created a Post (using models) before deploying. It had an image located a ./static/contents/Screenshot_334.png After deploying this image is accessible at https://someusername.onrender.com/static/contents/Screenshot_334.png . But if I create a new post from the deployed site I get a 404 error. The site doesn't cause any issue while its in the development mode.
Here's a portion of LOGS from Render:
https://someusername.onrender.com/static/contents/Screenshot_334.png (created while development) is accessible but https://someusername.onrender.com/static/contents/Screenshot_1.png (created post-production) is inaccessible. Also, static files (css, js) working fine.
I checked all the files and directories using the following piece of code:
res = []
for path, subdirs, files in os.walk('./'):
for name in files:
res.append(os.path.join(path, name))
Screenshot_334.png and Screenshot_1.png both are in the same directory
I also tried switching MEDIA_URL and MEDIA_ROOT to 'contents', 'contents/', 'static/contents/' and all the possible variations.
UPDATE:
I have found a workaround since I couldn't figure it out myself. I have mapped media to the urls.py and used FileResponse
Here's how views.py looks:
from django.http import FileResponse
def media(request, path:None):
img = open('./contents/'+path, 'rb')
response = FileResponse(img)
return response
I'm aware this might cause security issues but I think it won't do me any harm since it is a project just for learning.
Add whitenoise in your installed apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'whitenoise',
'accounts',
]
then add whitenoise.middleware.WhiteNoiseMiddleware in your middleware right after the first line.
try configring the static files as below in your settings.py
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / "staticfiles"
STATICFILES_DIRS = [
BASE_DIR / "static"
]
MEDIA_URL = 'contents/'
MEDIA_ROOT = BASE_DIR / 'static/contents'
edit your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path(....)
]
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)

How do you make Django search for templates in project root directory?

Right now
If I added these setting to settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'project',
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
It will search for index.html in
where_manage_py_is_at/project/templates/index.html
not
where_manage_py_is_at/templates/index.html
which is very perplexing since
os.path.join(BASE_DIR, 'templates'),
refers to
where_manage_py_is_at/templates/index.html
Can someone tell me why is django doing this?
Thanks
If you want to do like this,try
in manage.py #at top of file like
#!/usr/bin/env python
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
in settings.py
from manage import BASE_DIR as T_BASE_DIR
TEMPLATE_DIRS = (
os.path.join(T_BASE_DIR, 'templates'),
)

Django admin template is broken in production

I'm working with django, and trying to deploy my app to heroku.
All is working without any problems in local (even with DEBUG=False), but when deployed to heroku, the admin template doesn't display when DEBUG=False.
I followed theses instructions to configure my settings.py : https://devcenter.heroku.com/articles/django-assets
And here is my Procfile :
web: gunicorn bourse_logements.wsgi -b 0.0.0.0:$PORT
Feel free to ask if yu need some parts of my settings.py, I will paste them
Any help would be appreciated
EDIT :
Here is my settings.py :
https://gist.github.com/e-goz/62f812ab1fa8f8268f94
Are you sure you didn't .gitignore the templates folder?
* Add This line to your setting.py*
ADMIN_MEDIA_PREFIX = '/static/admin/'
you have to also copy all admin CSS and JavaScript to your static path(within static folder)
like static/admin/"you staticfiles"
You can try This setting as per your configuration on setting.py.
from unipath import Path
PROJECT_DIR = Path(__file__).ancestor(3)
PROJECT_ROOT = Path(__file__).ancestor(2)
sys.path.insert(0, Path(PROJECT_ROOT, 'apps'))
MEDIA_ROOT = PROJECT_DIR.child("media")
MEDIA_URL = '/media/'
STATIC_ROOT = PROJECT_DIR.child("collected_static")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
PROJECT_DIR.child("static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
TEMPLATE_DIRS = (
Path(PROJECT_ROOT, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.flatpages',
)
# Heroku specific settings
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# You can also try in your url.py
if settings.LOCAL_DEV:
baseurlregex = r'^media/(?P<path>.*)$'
urlpatterns += patterns('',
(baseurlregex, 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

Syntax Error when when running collectstatic in settings.py (Django 1.5.1)

I'm absolutely brand new to Python/Django/coding so I know there's probably something super simple that I'm missing. Any help in sorting this out would be awesome. Thanks in advance.
When I run python manage.py collectstatic I get in Terminal:
File "/Users/user/Desktop/mvp_landing/mvp_landing/settings.py", line 123
INSTALLED_APPS = (
^
SyntaxError: invalid syntax
In my settings.py file I have this at line 123, INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'south',
'join',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Here's the rest of the file (minus db info and such):
MEDIA_ROOT = "os.path.join(os.path.dirname(os.path.dirname(__file___))", "static", "media"
MEDIA_URL = '/media/'
STATIC_ROOT = "os.path.join(os.path.dirname(os.path.dirname(__file__))", "static", "static-only"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"os.path.join(os.path.dirname(os.path.dirname(__file__))", "static", "static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
SECRET_KEY = 'xxxxxxxxx'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mvp_landing.urls'
WSGI_APPLICATION = 'mvp_landing.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "templates",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'south',
'join',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
You are missing a parenthesis on the previous lines:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "templates",
)
I count 4 opening parens, but only 3 closing; you are not closing the os.path.join() call:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "templates"),
# missing parens ---------^
)
In Python, when you get a Syntax Error that doesn't immediately make sense, check the preceding lines to make sure you have your braces and parentheses properly balanced. For every ovening (, { or [ there must be a matching closing ), } or ].

Categories

Resources