I use django-compressor 1.1.2 with Django 1.4 when using compress templatetags PROJECT_PATH appended to STATIC_URL after compression
/static/Users/sultan/.virtualenvs/mediabox/somedia/somedia/public/media/media-cache/compressor/css/1d7cd4216904.css
When I don't set
COMPRESS_OUTPUT_DIR = os.path.join(MEDIA_CACHE, 'compressor')
Output looks like
/static/CACHE/css/1d7cd4216904.css
settings
STATICFILES_FINDERS = (
...
'compressor.finders.CompressorFinder'
)
COMPRESS_ENABLED = True
COMPRESS_OUTPUT_DIR = os.path.join(MEDIA_CACHE, 'compressor')
What is wrong with my configuration?
Thanks,
Sultan
According to the doc
django.conf.settings.COMPRESS_OUTPUT_DIR
Default : 'CACHE'
Controls the directory inside COMPRESS_ROOT that compressed files will be written to.)
I don't know what's your MEDIA_CACHE setting here, but you don't need to join directories to generate COMPRESS_OUTPUT_DIR, just give it a relative name such as 'compressor' or 'cache' to try.
Related
Was looking for a Python package for Django to manage assets, using Sass to compile CSS, and also cache busting, and Django-Assets / Webassets was recommended. Having trouble getting it setup though with my directory structure.
By default it looks for assets.py in each installed app. I want to set it up so that it sits in the same directory as settings.py and compiles app specific assets from each app directory into /static/js and /static/css.
I have django_assets in INSTALLED_APPS. According to the docs it looks like I needed to add this to settings.py:
ASSETS_MODULES = [
'project_dir',
]
Or:
ASSETS_MODULES = [
os.path.join(BASE_DIR, 'project_dir'),
]
Or:
ASSETS_MODULES = [
PROJECT_ROOT
]
At any rate, it just returns No asset bundles were found. If you are defining assets directly within your template, you want to use the --parse-templates option.
Even moving the assets.py into one of the app directories, it is looking in BASE_DIR/scripts which is where I keep my manage.py. Again, changing ASSETS_ROOT doesn't really same to be doing anything.
~/portal-client
project_dir
apps
account
templates
account
login.html
forms.py
urls.py
views.py
home
templates
home
home.html
urls.py
views.py
results
assets.py
settings.py
urls.py
scripts
manage.py
static
templates
base.html
footer.html
title.html
Couple of quick notes, for ASSETS_MODULES, the key word is additional:
django-assets will automatically look for assets.py files in each application, where you can register your bundles. If you want additional modules to be loaded, you can define this setting. It expects a list of importable modules:
i.e., we only use ASSETS_MODULES if we have assets.py files outside of any application. In your case, we will only specify this if project_dir is not an INSTALLED_APP.
Second, when using ASSETS_MODULES, you specify a dotted module path, not a directory. In your case, this would be project_dir.assets only if project_dir is not already part of INSTALLED_APPS.
i'm new in python world and i'm using python 3.4 and django 1.7
When o put DEBUG=True in settings.py the browser shows me an error like:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\workspace_virtualenv34\prog\prog\templates\main_sites\registration\login.html (File does not exist)
But my templates are in a different path:
C:\workspace_virtualenv34\prog\templates\
In settings.py i set ROOT_PATH as:
ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname( file ), os.pardir))
Is there anyway to turn back path levels that solve this problem?
Thanks.
You should be able to provide any directories to TEMPLATE_DIRS that django will use to search for templates
https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs
you should be able to construct this using the ROOT_PATH to avoid referencing absolute paths directly; change for your specific path:
TEMPLATE_DIRS = (
os.path.join(ROOT_PATH, '..', '..', 'prog', 'templates'
)
I have a model where I I am writing a function to upload files :
def get_upload_file_name(instance, filename):
return "my_files/{}".format(filename)
In my settings I have
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
My static structure
static
css
img
js
my_files
Here I dont want to return hardcoded my_files I want to return from setting like
{{MEDIA_ROOT}}/{}".format(filename)
or something like that...
How can I do this ?
To answer the question you actually asked, you can access your MEDIA_ROOT setting (and any other Django settings) in your views and models with:
settings.MEDIA_ROOT
As mentioned however, you probably want to read up on how media is handled in Django first.
Read about using settings in python code in the docs.
I figured it out!
You can access like this
modelName.fieldName.field.upload_to
I'm getting a 400 error when trying to access the /admin/filebrowser/browse/ page. I followed the instructions as per https://django-filebrowser.readthedocs.org/en/3.5.2/quickstart.html and have my URLs and installed apps configured correctly.
What I'm not too sure about are the media paths in settings.py;
FILEBROWSER_DIRECTORY = os.path.join(BASE_DIR, '/ogencat/MEDIA/uploads')
FILEBROWSER_MEDIA_ROOT = os.path.join(BASE_DIR, '/ogencat/MEDIA')
FILEBROWSER_MEDIA_URL = '/MEDIA/'
I have folder in my workspace called MEDIA and a folder within called uploads.
I wasn't too sure about what the docs wanted me to do in terms of setting these paths - I hadn't seen the getattr(settings, "FILEBROWSER_MEDIA_ROOT", settings.MEDIA_ROOT) syntax before so I just added the paths as I have done for the rest of settings.py
Thanks!
You need to add trailing slashes
Directories must exist prior accessing them
I've having a little issue with Django's staticfiles app.
I have added
'django.contrib.staticfiles',
to my INSTALLED_APPS and have added
STATIC_URL = '/static/'
STATIC_ROOT = '/Users/kevdotbadger/django/mylook/static/'
to my settings.py file.
All my static files are located within the STATIC_ROOT folder on my Mac.
Now, within my template I use
{{ STATIC_URL }}
which correctly renders to /static/.
However
{{ STATIC_URL }}css/style.css
result in a 404 error. I'm using the 'runserver' command as the server.
Is there something I'm missing?
I implore you to read the howto docs here: http://docs.djangoproject.com/en/dev/howto/static-files/
In short: STATIC_ROOT is only used if you call the collectstatic manangement command. It's not needed to add the directory to the STATICFILES_DIRS setting to serve your static files!
During development (when the automatic serving view is used) staticfiles will automatically look for static files in various locations (because you call its "serve" view with a path to a static file). For this search it'll use the so called "finders" (as defined in the STATICFILES_FINDERS setting).
One of the default finders is the AppDirectoriesFinder, which will look in the "/static/" directory of each of the apps of yours INSTALLED_APPS setting.
Another default finder is the FileSystemFinder, which will look in directories you specify in the STATICFILES_DIRS setting.
BTW, both these search patterns are similar to how template loading works.
The same technique will be used when running the collectstatic command, except that it now will collect the files from the various locations (using the same finders as above), putting the found files into STATIC_ROOT, ready for deployment.
If you want just a solution - scroll down to the "Solution".
Overview
I was discovering the same problem yesterday. Here is what I found:
All you need is appropriate static finder, that will find your STATIC_ROOT and all its contents, but there is no such finder. Here are default finders:
django.contrib.staticfiles.finders.AppDirectoriesFinder - search installed django applications dirs for 'static' folder, but most of them use obsolete 'media' folders for now.
django.contrib.staticfiles.finders.FileSystemFinder - use all dirs mentioned in the STATICFILES_DIRS, but you can't add STATIC_ROOT into it.
django.contrib.staticfiles.finders.DefaultStorageFinder - search static in your DEFAULT_FILE_STORAGE which is django.core.files.storage.FileSystemStorage by default and it points to your MEDIA_ROOT
Solution
That's all, no additional choices. There are no choices to use STATIC_ROOT for now (in Django 1.3).
So I've just wrote my own custom finder for these needs:
My custom static finder file: finders.py:
from django.core.files.storage import FileSystemStorage
from django.contrib.staticfiles.finders import BaseStorageFinder
from django.conf import settings
class StaticFinder(BaseStorageFinder):
storage = FileSystemStorage(settings.STATIC_ROOT, settings.STATIC_URL)
settings.py:
STATICFILES_FINDERS = (
'finders.StaticFinder',
)
If you want to use it with another finders - I suggest to put them after it inside STATICFILES_FINDERS
And remember: this solution should be used only in development needs and never on production!
I found a quick and easy workaround to serve project global static files during development:
Start a new app that contains your projects static files (e.g. "manage.py startapp static_content")
Create a folder named 'static' in that app and put your static files there.
Add your new app to the list of installed apps
First of all, make sure that you have static serve enabled in your urls.py FOR DEVELOPMENT ONLY.
Also, you may have an extra slash in there. If your STATIC_URL == '/static/', then
{{ STATIC_URL }}/css/style.css should be {{ STATIC_URL }}css/style.css.
STATIC_ROOT = '/home/ws/be/bla/'
STATICFILES_DIRS = ('/home/ws/be/static/',)
STATIC_URL = '/static/'
This works for me. STATIC_ROOT must differ from STATICFILES_DIRS (Django version 1.4 pre-alpha SVN-16436)