I want to serve the static content generated by django in a different host with nginx.
The server with the files has the name 'static-server' and I can't access it with django.
The static file looks like this:
STATIC_URL = 'http://static-server/static/'
But the error log gives the following path:
"GET /http:/static-server/static/admin/css/base.css HTTP/1.1" 500 145
Why is django looking for the files in local? How can I avoid this?
Long story short, STATIC_URL cannot point to remote directories without the use of extra plugins.
If you want to serve static content generated by Django with another web server (ie: Nginx) it must be in the same host.
Related
I have a basic file upload system written using django. It works and the files are properly uploaded to the correct path. However, when using google drive viewer it comes up as preview not available. After some digging around I figured the problem must be that the url is not being found, which it is not.
In other words mysite.pythonanywhere.com/media/files/myfile.pdf returns a 404 error.
However, http:127.0.0.1:8000/media/files/myfile.pdf works perfectly fine (on local server of course). So this is a problem with serving the files in production.
My media url is correctly mapped to the media directory, as shown on the pythonanywhere help page.
URL = /media/
DIRECTORY = /home/name/mysite/media
The files are saved in the correct place on the filesystem. My static files are served fine and have a near identical setup to the media files.
I've been trying to solve the problem for two days now but haven't found a solution. Any ideas?
In my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL ='/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
It could be worth noting that when I intentionally place the wrong path to a static file, the 404 error doesn't include a list of urls which django has searched. It does in the 404 error messages for media. So the server is directly serving the static files without passing the request down to the wsgi layer, but for some reason it's not handling media urls the same way? By the way I am working with debug=False but only set it to True to help diagnose the problem. Here are the messages:
Request Method: GET
Request URL:http://infodump.pythonanywhere.com/static/randfile.txt
'randfile.txt' could not be found
Request Method: GET
Request URL: http://infodump.pythonanywhere.com/media/randfile.txt
Using the URLconf defined in InfoDump.urls, Django tried these URL patterns,in this order:
1.^admin/
2.^streamer/
3.^ ^$ [name='home_page']
4.^ ^signUpPage/$ [name='sign_up']
The current URL, media/randfile.txt, didn't match any of these.
I'm just at my wits end here. I want to load my custom js files from the static folder.
When I view the source on Chrome, it's not showing up and it's not being loaded when I refresh the page.
What it looks like on Chrome (and I can't click on my file, while the jQuery does light up):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"/>
<script src="/static/js/ajax.js"/>
UPDATE:
Here is what I have now, but it's still not working
base.html
{% load staticfiles %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"/>
<script src="{% static 'js/ajax.js' %}"/>
settings.py
STATIC_ROOT = 'static/'
STATIC_URL = 'assets/'
STATICFILES_DIRS=(BASE_DIR, 'assets')
I ran python manage.py collectstatic and it copied a bunch of files over.
Here is a layout of the updated structure. You can see that the ajax.js was copied over but it's still not showing up when I run the server. Any ideas?
What am I missing?? Please help.
Python 2.7, Django 1.7
Obligatory reading:
https://docs.djangoproject.com/en/1.7/howto/static-files/
https://docs.djangoproject.com/en/1.7/ref/contrib/staticfiles/
https://docs.djangoproject.com/en/1.7/ref/settings/#static-files
About these settings:
STATIC_URL = 'assets/'
This means that your app should serve static files with assets/{name} prefix.
I think this is the source of your problem - the initial / is missing and you're generating relative links for static resources, which sounds like a Bad Idea.
Now a page at http://yourserver/foo/bar/ would ask for static files via a relative link:
<img src="assets/{name}">, so your browser would look in /foo/bar/assets/{name} and find nothing.
You'd normally want an absolute link for static files, so instead you should use STATIC_URL = '/assets/' and obtain absolute links <img src="/assets/{name}">.
STATICFILES_DIRS=(BASE_DIR, 'assets')
Okay, so how does Django find the static files? A Django project can have many installed applications, and each application can have its own directory with static files.
So there's this setting (set by default):
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder")
This means that, on development setup (runserver), whenever someone asks for http://yourserver/assets/ponies/applejack.jpg (where /assets/ is your, now hopefully fixed, STATIC_URL), Django would try to find a file ponies/applejack.jpg in the following way:
first by using FileSystemFinder that looks in some fixed directories you specify (STATICFILES_DIRS),
second by using AppDirectoriesFinder that looks into the static/ subdirectory of each of your INSTALLED_APPS.
So if you want to keep your static files per-app, you don't need to rely on FileSystemFinder and you can leave STATICFILES_DIRS empty.
But if you want to keep all the static files in one place, point STATICFILES_DIRS to any directory like you did. You should still keep AppDirectoriesFinder for third party apps you use, like the Django Admin, that come with their own static files.
STATIC_ROOT = 'static/'
This is unnecessary for development setup with runserver because Django dev server serves your static files itself.
For production, however, you probably want your web server (Apache, Nginx, ...) or a content delivery network to handle the static file delivery for you. You can then use the collectstatic command to collect all the static files from everywhere (e.g. all static files your STATICFILES_FINDERS can find), and put them all in one directory on your machine, from where you can easily pass them on to Nginx or copy over to a CDN.
Do you have this in your settings?
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# django.contrib.staticfiles.finders.DefaultStorageFinder',
)
I'm trying to compile SASS and compress CSS files with django pipeline on Django 1.6.3, but I get the following error after visiting my site:
ValueError: The file 'css/test2.css' could not be found with <pipeline.storage.PipelineCachedStorage object at 0x0585DF50>.
I configured pipeline following the guide on readthedocs.org: I added pipeline to INSTALLED_APPS then I defined STATIC_URL and STATIC_ROOT:
STATIC_URL = '/test/forum/skins/default/media/'
STATIC_ROOT = '/test/forum/skins/default/media/'
Folders tree:
test
forum
skins
default
media (static files)
css
js
images
site1
site2
site3
views
utils
settings.py
I added SASSCompiler to PIPELINE_COMPILERS and then I added the path to the file to be compressed:
# pipeline settings
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_COMPILERS = (
'pipeline.compilers.sass.SASSCompiler',
)
PIPELINE_CSS = {
'main': {
'source_filenames': (
'css/test.scss',
),
'output_filename': 'css/test2.css',
},
}
Finally I linked the css to my XHTML index:
{% load compressed %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
{% compressed_css 'main' %}
</head>
I do not understand what I did wrong.
Thanks for any help!
UPDATE:
When I run collectstatic this copy the files from django and not from my project
F:\DEV\DJANGO\apps\test>python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
F:\test\forum\skins\default\media
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Copying 'F:\DEV\DJANGO\apps\django\django\contrib\admin\static\admin\css\base.
css'
Copying 'F:\DEV\DJANGO\apps\django\django\contrib\admin\static\admin\css\chang
elists.css'
Copying 'F:\DEV\DJANGO\apps\django\django\contrib\admin\static\admin\css\dashb
oard.css'
Copying 'F:\DEV\DJANGO\apps\django\django\contrib\admin\static\admin\css\forms
.css'
Copying 'F:\DEV\DJANGO\apps\django\django\contrib\admin\static\admin\css\ie.cs
s'
[etc .... ]
But the path F:\test\forum\skins\default\media is wrong, my project is located
in F:\DEV\DJANGO\apps\test\forum\skins\default\media.
Then I tried to find the static file:
F:\DEV\DJANGO\apps\test> python manage.py findstatic css/main.css
No matching file found for 'css/main.css'.
But the file exists.
In Django's settings.py, the STATIC_ROOT variable tells Django where to move your static files, whereas STATIC_URL is the URL path that users will see and from where you can access the files within the browser. See the Django documentation on static files:
https://docs.djangoproject.com/en/dev/ref/settings/#static-files
For example, lets's say you have the following in your settings.py file:
STATIC_ROOT = "/var/www/example.com/static/"
STATIC_URL = "/static/"
Now, let's you are doing your software development on the same machine within /home/fuiba/git/your-django-project/. Within your Django project you have a sub-directory called static, and within that directory you have a file called custom.css. The full path to this file is:
`/home/fuiba/git/your-django-project/static/custom.css`
Now, after running python manage.py collectstatic your style sheet is moved to your STATIC_ROOT directory. Now you start the web server and you can access the static file via the project's STATIC_URL. So, let's assume you run the web server on the localhost on port 8000, you can access the stylesheet from your browser at the following location:
http://localhost:8000/static/custom.css
Ok, that's the foundation for how it works. In your case, you appear to be developing under Windows, so there's a few caveats. First, don't use backslashes ('\') in your paths, use only forward slashes. However, you still must specify the drive letter and colon. So, let's set your STATIC_ROOT to:
STATIC_ROOT = "F:/DEV/DJANGO/apps/test-static-root/"
I'm choosing this so you don't pollute your test project directory, and a sibling directory should be created in the above location with your static files within.
Let me know how you get along, and I'll try to provide more details.
Good luck!
I've been trying to follow every single tutorial and thread there is, i jast cant get it togheter.
Sorry, but please exlpain it to me as if i was three years old..
What i have:
a CSS that i'd like use, styling a template.
How to get there:
MD: project/app/static/
place style.css there
settings.py:
STATIC_ROOT = '/project/app/static/'
STATIC_URL = '/static/'
(Should'nt this at least give me the string '/static/', when i try printing {{ STATIC_URL }} in the template..?)
template:
<LINK REL=StyleSheet HREF="{{ STATIC_URL }}/style.css" TYPE="text/css" MEDIA=screen>
Seems to me, the next logical thing would be to create an /static/-url aswell?
I'm really lost here..
Django's staticfiles system is about two things:
Combining static files from a number of (app-specific) directories into a single place from which they can be served
Serving those files over HTTP
You want #1 if you have a number of static files located in different directories. You want #2 if you want to see those files while you are using the development server. In production, you usually skip #2, because the purpose of #1 is to put the files somewhere that your web server can access them.
Organizing and collecting static files
With that said, here's how you get #1:
settings.py:
# This is a filesystem path
STATIC_ROOT = '/path/to/my/project/collected-static-files/'
# This is a URL prefix
STATIC_URL = '/static/'
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
...
)
At this point, you can do a few things:
You put your static files in an '/static/' directory under each of your apps. That's where django will look for them. There is an additional setting, STATIC_DIRS, that you can use if you have static files that are outside of your apps.
You can run manage.py collectstatic to have Django go through all of your apps, finding all of the static files, and copying them into /path/to/my/project/collected-static-files (That's why that setting is a filesystem path.)
You can use {{ STATIC_URL }} in templates to access the files.
Serving static files over HTTP
In development mode, under the dev server, Django will automatically serve files out of the STATIC_ROOT directory.
Basically, Django will add a URL handler for URLs of the form
/static/some-dir/some-file-name
And serve content from your hard drive, from the filesystem path
/path/to/my/project/collected-static-files/some-dir/some-file-name
In production, this won't happen automatically, so you will have to configure your web server to do the same thing. (It's just not a good idea to run all of your static files through Django's full processing pipeline. The web server can do it much more efficiently)
I am trying to serve static files from another domain (sub domain of current domain).
To serve all media files I used this settings:
MEDIA_URL =
'http://media.bud-inform.co.ua/'
So when in template I used
{{ MEDIA_URL }}
it was replace with the setting above. Now I am trying to serve admin media files from the same subdomain, I changed the settings this way:
ADMIN_MEDIA_PREFIX =
'http://media.bud-inform.co.ua/admin_media/',
and expected that all calls to media from my admin site will be made to this url.... But actually it didn't work this way, I still see paths to CSS made as following:
http://bud-inform.co.ua/media/css/login.css
Could you suggest how to serve admin media files correctly
MEDIA_URL and ADMIN_MEDIA_PREFIX are two different things. One is the location of your media files, while the other is the location of the django admin system's media files.
You have to make sure that the ADMIN_MEDIA_PREFIX points to somewhere where you're actually serving the admin media. Django doesn't handle that step for you.
The django admin media is at django/contrib/admin/media/. Copy or symlink that directory somewhere publicly visible and set ADMIN_MEDIA_PREFIX to reflect where you put it.