I cahnged the static files defualt path
and added in url.py
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static")
It works fine for first level pages
like
/login
/admin
/dahboard
but static files does not load when I go to second level pages
/admin/users/
/admin/users/add/
How I can fix this problem
Make sure your STATIC_URL has a / at the beginning:
STATIC_URL = '/static/'
Otherwise, URL will be like <img src="static/thing.png" /> which can work on first level (since it will search from the root) but not when you are in subdirectories.
Be aware the serve method only work in DEBUG mode.
Related
When I am reqeuesting media files (.png in this case) in the media folder in development I get this error:
AttributeError at /media/test.png
This FileResponse instance has no content attribute. Use streaming_content instead.
Request: http://localhost:8000/media/test.png
I added below to the url patterns:
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And below to the settings:
STATIC_ROOT = BASE_DIR / 'static'
STATIC_URL = '/static/'
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
Static files are working just fine.
What do I overlook?
Thanks!
hello if you have debug set to True you have to use this:
first go to you settings and set all you static file directories in
STATICFILES_DIRS = [
list of your static file,.......,]
the second step you should have this in your urls.py
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATICFILES_DIRS[0])
note:there is a difference between static_root and staticfile_dirs
static_root is only used for production
this is my mail for more explanation :nguewofoss#gmail.com
I found the problem.
In the newer django version staticfiles are rendered with a streamingHttpResponse, this doesn't have a content attribute.
I found below code in my code that points to the (non existing) content attribute:
if b'<html' in response.content[:100].lower():
I added this to resolve the problem:
if not hasattr(response, 'content'):
return response
if b'<html' in response.content[:100].lower():
Thanks all!
I am running Django project at suburl like www.example.com/django . Everything is working fine but my static files are not working on this suburl since they the url they are taking is main like www.example.com/static/path_to_files but it should take the url as www.example.com/django/static/path_to_files.
This can be the case for other urls too because I think whenever I use any url for any link it must take the hostname with the suburl like www.example.com/django instead of www.example.com .
you can also see this question of mine for more information about server configuration files.
In settings.py try:
STATIC_URL = '/django/static/'
More information here: https://docs.djangoproject.com/en/dev/howto/static-files/
You can serve them manually during development by adding the lines to urls.py:
urlpatterns = patterns('',
# Media files
url(r'^media/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}
),
# Static files
url(r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.STATIC_ROOT, 'show_indexes': True}
),
)
Don't use this in production, and read the doc, its well explained.
Define MEDIA_URL and STATIC_URL in settings.py
MEDIA_URL = /django/media
STATIC_URL = /django/static
Then prepend this variable while defining url in templates:
media
<img src="{{ STATIC_URL }}/path_to_static_file" />
Now you can use these variables wherever you want to create urls.
I wanted to enable serving static files through django 1.5 builtin development server. I came across a strange problem.
If I make a request to my static file using localhost:8000/static/staticstyle.css then it responds me with 404 not found. But If I make a request to the same file without the value mentioned in the STATIC_URL and add the url pattern as "url(r'^anyval_other_than_mentioned_STATIC_URL/', django.views.static.serve,{'document_setting':settings.STATIC_ROOT}), ", then it responds me with 304 , which is conditional get.
I have collected all the static files in the static directory setup in the settings file and enabled all the context processor required for the template.
For production I have used nginx to serve the static file so there is no problem.
My settings.py looks like
STATIC_ROOT = APPLICATION_PATH+"/static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('vendors',APPLICATION_PATH+'/vendors'),
('admin/assets/',APPLICATION_PATH+'/templates/admin/assets'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
My application urls.py looks like
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Any hints would be highly appreciated.
Thank you .
please read this first
https://docs.djangoproject.com/en/1.5/howto/static-files/
There are some settings related to static files serve
The list of finder backends that know how to find static files in various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Absolute path to the directory static files should be collected to
STATIC_ROOT = os.path.join(APPLICATION_PATH, '..', 'static')
URL prefix for static files.
STATIC_URL = '/static/'
You can specify Additional locations of static files
STATICFILES_DIRS = (
os.path.join(APPLICATION_PATH, 'vendors'),
os.path.join(APPLICATION_PATH, '/templates/admin/assets'),
)
There is special application for static serve in django contrib
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
After You have configured your project run collectstatic command
python manage.py collectstatic
This will copy all files from your static folders into the STATIC_ROOT directory.
i have a problem with django static files handling.
I am confused about how to use MEDIA_ROOT, STATIC_ROOT, MEDIA_URL and STATIC_URL
I have a file structure like this(sorry, i dont know how to indent properly :S):
static/
css/
img/
js/
For example, if I have a .css file in my css dir, how could I reach it?
MEDIA_ROOT and MEDIA_URL are deprecated. Use STATIC_ROOT and STATIC_URL.
STATIC_ROOT = "Absolute path to your static directory" (Example: /home/your_app/static)
STATIC_URL = "/static/" (Could be anything you would like to use)
When you want to serve static content pass the context_instance and in your template use,
{{STATIC_URL}}/file_path_from_static_directory/
EDIT:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
The media is currently on my local development machine.
My MEDIA_ROOT, MEDIA_URL, ADMIN_MEDIA_PREFIX and are specified as below:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "media")
MEDIA_URL = '/media/'
SITE_URL = 'http://localhost:80'
ADMIN_MEDIA_PREFIX = '/media/admin/'
There is no 'admin' folder but that shouldn't make a difference I don't think.
In the urls.py file I have:
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
I am at a loss as to what I should do to get it working.
[I am trying to learn django and am working with an existing project that's pretty hairy]
You're mixing and matching pre and post-Django 1.3 static file handling. Originally all static files were served from MEDIA_URL, but Django 1.3 introduced the staticfiles contrib package and the associated STATIC_ROOT and STATIC_URL settings. django.views.static.serve utilizes the new staticfiles app, which you haven't set up.
Assuming you're running Django 1.3, first, you'll need to add 'staticfiles' to your INSTALLED_APPS. Then, you'll need to define STATIC_ROOT and STATIC_URL. The standard location is a project-root level directory named "static".
You'll also need to add the staticfiles template context processor:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
)
This will make the STATIC_URL variable available in your templates, so you can reference your resources with something like {{ STATIC_URL }}css/style.css
All your static resources will also need to go into an app(s)-level directory named "static". The actual project-root level "static" directory is never directly used. It's simply the place where the collectstatic management command dumps all your static resources for use in production.
If you want project-wide static resources (not tied to any one particular app), you'll need an entirely separate directory (i.e. not the same as MEDIA_ROOT or STATIC_ROOT). I tend to use one named "assets". You'll then need to tell Django to look in here for static resources as well with the STATICFILES_DIRS setting:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'assets'), # or whatever you named it
)
MEDIA_ROOT/MEDIA_URL are now only used for user uploads (e.g. any file created through FileFields and ImageFields, so you still need it, but you won't ever manually store anything there.
When you reach production, your webserver will need to serve both MEDIA_ROOT and STATIC_ROOT at MEDIA_URL and STATIC_URL, respectively. You'll also need to run:
$ python manage.py collectstatic
To make Django compile all your static files into the directory specified by STATIC_ROOT.
works with django 1.8 - 1.11:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development
note that Django documentation states that this is
not suitable for production use
(obviously unless you use if settings.DEBUG: part)
On development server, this page may help you.
https://docs.djangoproject.com/en/1.2/howto/static-files/
By adding follow code to urls.py:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
)
With python-django 1.7 I used
if settings.DEBUG:
urlpatterns = patterns('',
(r'^$', 'blenderx3d.first_step.views.index'),
(r'^media/(?P<path>.*)$','django.contrib.staticfiles.views.serve'),)