45
Use finders-module of Django
from django.contrib.staticfiles import finders
result = finders.find('css/base.css')
searched_locations = finders.searched_locations
String result
is the file-system path, and if not found, double check searched_locations
23
project_dir.url add to the end of file
if DEBUG:
urlpatterns += patterns(
'',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': STATIC_ROOT}),
)
project_dir.settings
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_debug')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
make dirs media & static_debug (add them to .gitignore)
project_dir/
static/
image.jpg
settings.py
urls.py
apps/
__init__.py
some_app/
static/
some_app/
css/
style.css
media/
static_debug/
Now you can run project or directly
python manage.py collectstatic
access from views
from django.templatetags.static import static
static('image.jpg')
static('some_app/css/style.css')
access from templates
{% load staticfiles %}
{% static 'image.jpg' %}
{% static 'some_app/css/style.css' %}
- [Django]-Django media URLs in CSS files
- [Django]-How to check if ManyToMany field is not empty?
- [Django]-Oauth for Google API example using Python / Django
7
After doing lots of mistakes the following thing worked for me..
Suppose you have following directory structure (Thanks @madzohan)
project_dir/
static/
image.jpg
settings.py
urls.py
apps/
__init__.py
some_app/
static/
some_app/
css/
style.css
And now if you want to get the path of project_dir/static/image.py then
In your project_dir/settings.py file define a STATIC_DIR varaible on the file scope like. ( If not done before )
project_dir/settings.py
import os
# after your other file variables
STATIC_DIR = os.path.join(BASE_DIR, 'static')
Now in your app_name/views.py file from where you want to acess the /static/image.jpg file
app_name/views.py
import os
from project_dir.settings import STATIC_DIR
# here I want the /static/image.jpg file then
image = os.path.join(STATIC_DIR, 'image.jpg')
And then you can have acces to image.jpg file throught image variable
Hope this works well for you
- [Django]-Which database engine to choose for Django app?
- [Django]-Django 1.8 Run a specific migration
- [Django]-How do I access the properties of a many-to-many "through" table from a django template?
2
Since you want an app’s static, here’s what you need!
from django.contrib.staticfiles import finders
APP_LABEL = 'app_youre_looking_at'
FILE_NAME = 'file_name_you_want_to_read_or_write.ext'
stores = finders.AppDirectoriesFinder(app_names={APP_LABEL}).storages
print(f'Here it is: {stores[APP_LABEL].path(FILE_NAME)}')
The FILE_NAME
is not required to exist. You can use stores[APP_LABEL].path('')
to get path only.
- [Django]-How to test 500.html error page in django development env?
- [Django]-Embed YouTube video – Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
- [Django]-Django: How to pre-populate FormView with dynamic (non-model) data?