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.
Related
I'm working with an existing (and previously functional) Django site. We recently upgraded from Django 1.8.13 to 1.10 and our WSGI is Gunicorn. It works fine when hosted from my development machine, but when deployed, all static resources (on the admin and the main site) yield 404's with the message, Directory indexes are not allowed here.
Our settings.py contains the following:
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
DEBUG = True
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_resources')
The directory structure looks like this:
/my-project-name
/my-project-name
server.py
settings.py
urls.py
wsgi.py
...
/static
/static_resources
manage.py
Django does not serve static files in production mode (DEBUG=False). On a production deployment that's the job of the webserver. To resolve the problem:
run python manage.py collectstatic
in your web server configuration point the /static folder to the static folder of Django
Don't just turn DEBUG on, it would be dangerous!
Try to change os.path.join(PROJECT_DIR, '../static') to os.path.join(PROJECT_DIR, 'static') and STATIC_ROOT = os.path.join(PROJECT_DIR, '../static_resources') to STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_resources'). It will solve your problem.
The answer was very subtle. When I upgraded Django to 1.9 and ran the server, it gave the following warning:
?: (urls.W001) Your URL pattern '^static/(?P<path>.*)$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
In urls.py, my urlpatterns list contained:
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
I changed it to:
url(r'^static/(?P<path>.*)/', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
This eliminated the warning but caused static resources to stop loading.
It needed to be:
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
It's still a mystery to me why this worked on my dev machine (a Macbook), as well as another on the team's dev machine (a windows laptop), but not on our Linux server. But, it works now, so I'm done trying to figure it out.
Have a problem with connecting css to my template.
My project root is
"D:/birzha/", static path is "D:/birzha/static/", css in
"D:/birzha/static/css/template.css".
What STATIC_ROOT or STATICFILES_DIRS should I use for correctly viewing css file? I tried so much turns, but nothing happens, css still off.
First of all you need to tell where all your static files will be "collected", place the following lines in settings.py:
BASE_DIR = (os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Then you need to make a static folder within your app and the hierarchy
should be like this appname/static/appname/css.
Finally run the command python manage.py collectstatic and type yes on prompt. This will copy all of your static files within a folder that you specified in STATIC_ROOT
Now you can access your static files by giving the path like,/static/appname/css/mystyle.css
You can try this
Site root will contain you project root path. You can change "/templates" with static
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
# calculated paths for django and the site
# used as starting points for various other paths
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates/'),
)
MEDIA_ROOT = os.path.join(SITE_ROOT, 'templates/media/')
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(SITE_ROOT, 'templates/'),
)
I have two directories called media and static.
When I have DEBUG = True everything works but if I change it to DEBUG = False the problems arises.
I've read that my server will handle static files in production. I have understood it as
In development it's fine to have all my static files in my directory static but when I go in production I need to move all my static files to another directory (it could be static_www?) and before starting the server I run python manage.py collectstatic. This command will move all the files from static_www to static and everything works.
But why do I need to have two separate directories with same content? How do the server know that my files 'has been collected' through collectstatic (it's just files in a folder so how can it know the difference)? I guess collectstatic is primarily used when you have static files in multiple directories and want to collect them all before you go into production.
The variables in my settings.py looks like:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_www"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
and it moves from static_www to static so neither in production nor development my directory static_www is not used.
Maybe all these settings are meant for websites which has another server only for handling static files?
STATIC_ROOT is where the virtual directory for STATIC_URL will point.
Example:
STATIC_ROOT = '/var/www/example.com/static/'
STATIC_URL = 'http://www.example.com/static/'
collectstatic will find all static files in STATICFILES_DIR and copy to STATIC_ROOT.
A request to http://www.example.com/static/file.jpg will be handled by Django and /var/www/example.com/static/file.jpg will be served.
You can set your static files to be even in another site, using something like:
STATIC_ROOT = '/var/www/static.example.com/static/'
STATIC_URL = 'http://static.example.com/static/'
In this way, static.example.com can be a simple server which will serve files without they be handled by Django, increasing the site performance. Or you can upload to a CDN like Amazon S3 (and CloudFront), etc.
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.
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'),)