I have a django application that sends off an email containing image files as MIME attachments. The emails are sent from views.py, but in order to attach the files, I need to get the full path name of the image(s), so python can open them. These files are in the static folder in my app, but I can't seem to find a way that I can get the full path of the file on the filesystem that works in development mode - It works fine in production after collecting static, but in dev, it can't find the file as the static files are served from individual app folders in development.
Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.
STATICFILES_DIRS tells Django where to look for static files in a Django project, such as a top-level static folder. STATIC_ROOT is the folder location of static files when collectstatic is run. STATICFILES_STORAGE is the file storage engine used when collecting static files with the collectstatic command.
"STATIC_ROOT" sets the absolute path to the folder where static files used for the apps and admin in a django project are stored and this command below creates the folder and collects static files from the apps and admin in a django project into the folder (*Setting "STATIC_ROOT" never ever influence to static file URL ...
First off, you'll typically find these three types of files in a Django project: Source code: These are your core Python modules and HTML files that make up every Django project, where you define your models, views, and templates. Static files: These are your CSS stylesheets, JavaScript files, fonts, and images.
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
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' %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With