Django StaticFiles APP and Wsgi - django

It's not so easy questions as others.. at least. it's not a problem to setup simple one-dir-based static files location..
https://bitbucket.org/sirex/django-starter/src
There's such an interesting project in here.. this one is using distribute and buildout for making whole project and django with modules in one dir. you can migrate from dev to production mode easy and etc.. all you need is just to rename dir, and type "make" in it, and that's it =) there's manual in there...
Situation which works with python server, and don't work with apache mod_wsgi:
Default static files location is: "var/htdocs/static". This can be overridden with one static dir location for example apps/myapp/myapp/static/. This works with python webserver but doesn't work with wsgi/apache. wsgi can't see anything apart default directory.. example: http://localhost:8000/static/css/main.css works but with apache same url doesn't work. and this file lies in myproject/apps/myapp/myapp/static/css/main.css although default static dir is var/htdocs/static =)
As far as I understand this overriding made with StaticFiles application in settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'htdocs', 'static')
STATICFILES_DIRS = (
os.path.join(BUILDOUT_DIR, 'project', 'static'), # <-- why "project" and not "apps" I don't know X_X
)
maybe this one is incorrect, I don't know but with py-server this works. apache vhost works with default location.. and setuped to "var/htdocs/static".
Maybe problem is in wsgi script?
#!/usr/local/bin/python2.6
import os,sys
sys.path[0:0] = [
'/usr/local/lib/python2.6/site-packages/',
'/www/webapp/visimes/eggs/PIL-1.1.7-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/South-0.7.3-py2.6.egg',
'/www/webapp/visimes/eggs/django_annoying-0.7.6-py2.6.egg',
'/www/webapp/visimes/eggs/coverage-3.4-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/django_debug_toolbar-0.8.4-py2.6.egg',
'/www/webapp/visimes/eggs/django_extensions-0.6-py2.6.egg',
'/www/webapp/visimes/eggs/django_test_utils-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipdb-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipython-0.10.1-py2.6.egg',
'/www/webapp/visimes/eggs/djangorecipe-0.21-py2.6.egg',
'/www/webapp/visimes/eggs/zc.recipe.egg-1.3.2-py2.6.egg',
'/www/webapp/visimes/eggs/zc.buildout-1.5.2-py2.6.egg',
'/www/webapp/visimes/eggs/BeautifulSoup-3.2.0-py2.6.egg',
'/www/webapp/visimes/eggs/setuptools-0.6c12dev_r88795-py2.6.egg',
'/www/webapp/visimes/parts/django',
'/www/webapp/visimes',
'/www/webapp/visimes/project', # <-- this one need for monitor.py which i put in there
'/www/webapp/visimes/apps/portal', # <-- startapp.sh script some how forgot to add this dir, it's my default app dir, which must be generated with startapp.sh and added in here..
]
import djangorecipe.wsgi
if __name__ == '__main__':
djangorecipe.manage.main('project.development')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.development'
import monitor
monitor.start(interval=1.0)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I added last 4 lines by my self. because I couldn't start apache.. I'm guessing that djangorecipe.wsgi should handle everything else with staticFile override.. anyway please, check out that package, if you on linux or mac, and try it by your self. it must work
ps. (btw bin/django need to dublicated as bin/django.wsgi and etc/apache.conf is generated vhost for apache)
I'd really apriciated if somebody would try to launch this "Starter" manually with wsgi... then you'd understand everything.=)
Edit: Any information about how can WSGI understand where he needs to search static files apart default location from django settings, is REALLY appreciated =)

There is lots of documentation on the official mod_wsgi site for understanding how to use it. This includes how to set it up for serving static media files. See:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines

WSGI has nothing whatever to do with serving static files. This is all clearly covered in the Django deployment documentation - as Torsten suggests in the comments, you'll need to point Apache at your static files, probably via an alias.
I must say though that this project looks very dodgy. Manually adding a load of eggs to sys.path is not the right way to go about things - a much better way would be to use something like virtualenv, which manages all that for you.

No way of having some kind of overriding like django does with WSGI..
there's great command in django "manage.py collectstatic" which places all files from STATIC_DIR list (in settings.py) to main static directory.. in fact this commands just copy files from all those dir's and that's it =)
Would be great to know, how could I make this copying automatic when any file in that dir would be updated.. same thing like monitor.py for automatic wsgi reload when source is modified...

Related

Django Collectstatic Suspicious File operation

I am trying to run collectstatic on heroku. When I got this error:
remote: 'component ({})'.format(final_path, base_path))
remote: django.core.exceptions.SuspiciousFileOperation: The joined path (/tmp/build_4652acfe079723bc273763513a187201/fonts/glyphicons-halflings-regular.eot) is located outside of the base path component (/tmp/build_4652acfe079723bc273763513a187201/staticfiles)
I thought perhaps I had missed something with collectstatic on my end, so I ran it locally, and got the exact same error.
Then I went looking. I found:
/home/malikarumi/Projects/aishah/jamf35/staticfiles/bootstrap/fonts/glyphicons-halflings-regular.eot
and
/home/malikarumi/Projects/aishah/jamf35/static/bootstrap/fonts/glyphicons-halflings-regular.eot
My settings:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/bootstrap/fonts/'),
There is a ticket out there, but it seems to be about paths, and I see nothing wrong with my paths, https://code.djangoproject.com/ticket/27201
Another one deals with files, and might be closer to my issue, because it has to do with created tmp files, but I really can't tell:
https://code.djangoproject.com/ticket/26644
I should note that I also looked at Django: The joined path is located outside of the base path component,
Django: How to allow a Suspicious File Operation / copy a file, and Django | joined path is located outside of the base path component {% static img.thumbnail.url %}, Error 400 with whitenoise, but they seem to be more about MEDIA ROOT issues.
I'm not sure what the fix is, here. Please advise. Thanks.
Your STATICFILES_DIRS setting looks odd. Are you sure you don't want this?
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'),]
The problem is that one of your CSS files has a relative reference which is resolving outside of the static directory, and I think that's because you have static/bootstrap/fonts where you should just have static.

Static file cannot be found in Django view

I am having an issue with static files in the development server on Django 1.5.4. I am not sure if it is the same problem on the actual production server (running Apache), as I found a solution for that which works at the moment (simply hard coding the full URL - I know it's bad, but it gets the job done).
I am using Reportlab to create a PDF file for my project, and I need to include a picture on that. I followed the answer in a different post:
from django.templatetags.static import static
url = static('x.jpg')
Unfortunately, the answer I get from the server is an IO Error: 'Cannot open resource "localhost:8000/static/images/x.jpg"', even though a copy and paste of that into the URL bar clearly shows me that the picture is exactly there.
My settings regarding static files are the following, and they do work for everything else (CSS, Javascript, etc):
ROOT_PROJECT = os.path.join(os.path.split(__file__)[0], "..")
STATIC_ROOT = os.path.join(ROOT_PROJECT, 'static')
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Thanks for your help!
Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
There are usually a couple of ways to store static files.
One way is to create a static folder inside your app folder and store the files there. You can check that here:
Is to create a folder and store your static files which are not for any particular app.
From the django documentation:
Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.
For example:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
)
If you are into production then check production deployment for more details!

Django TEMPLATE_DIRS looking in the wrong directory?

The code for my web app is currently on dropbox and I simply change the TEMLPLATE_DIRS variables in the settings.py module when working on my work and home computers.
I have run into an issue this evening when firing up the app. I am getting a TemplateDoesNotExist error, here are the details:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/me/Dropbox/app/MyApp/Users/me/Dropbox/app/MyApp/templates/App/Page 1/pageone.html (File does not exist)
Basically the first /Users/me/Dropbox/app/MyApp shouldn't be there.
settings.py
TEMPLATE_DIRS = (
"Users/danielholmes/Dropbox/app/MyApp/templates/",
)
This is going to be something stupid i think - please let me know if more info is required.
Many thanks
Never hardocde the directory paths in settings files. Let Python generate the absolute path names for you. This makes your project portable across different environments. Below is a good approach to define paths (and may be solve your issue also):
import os, sys
abspath = lambda *p: os.path.abspath(os.path.join(*p))
PROJECT_ROOT = abspath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)
TEMPLATE_DIRS = (
abspath(PROJECT_ROOT, 'templates'),
)

Django 1.4 (MEDIA_ROOT, STATIC_ROOT, TEMPLATE_DIRS)

I have a Django 1.3 project with this options in settings.py
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates'), )
But in Django 1.4 by default settings.py is moved in subdirectory with name that is equal to project name. Because of that static, media and templates directories now have to be moved in the same subdirectory?
Is this what I have to do, or just change STATIC_ROOT, MEDIA_ROOT and TEMPLATE_DIRS options?
I know that both variants are OK, but what is best practice for this in Django 1.4?
And also I know that every app can have it's own templates and static directories.
And is it better to put all other application directories inside the same subdirectory? This is not what is happening by default using manage.py startapp
OK the scheme that I follow is this:
myproject/requirements.txt - pip installable packages
myproject/deployment - Deployment stuff like server config files, fixtures(dummy data), etc.
myproject/docs - project's docs
myproject/tests - project's tests
myproject/myproject - project's operational code(and settings.py, urls.py)
Expanding myproject/myproject folder:
myproject/myproject/app1 - a regular app(encompassing its specific templates/static files)
myproject/myproject/app2 - another regular app(same as above)
myproject/myproject/website - semi special app, by convention.
This website app houses basically 4 things:
1) an empty models.py(so that django will consider it as a valid app)
2) a views.py with the entry point index view. Maybe some other views that don't fit in any other specific app.
3) a management dir with custom django commands which apply to the whole project.
4) a templates dir that has the 404.html and 505.html. Also it has a subdir called website that includes universal/base html templates that every other app extends, plus
the index.html.
5) a static dir with subsequent subdirs named css, js and media for global static files.
Nothing exotic I guess. I think that most people follow a similar pattern, but I would like to here any inefficiencies with this, if any.
EDIT:
with regards to settings for production VS development I use
the popular settings_local pattern, which you can read here and eventually will
lead you here, which describes a better pattern.

Django dev server intermittently fails to serve static files

In my development environment I'm getting intermittent failures for serving static files (js scripts and css). In the error console in Chrome I get 404s. But if I refresh on those items, or visit the URLs directly, they're served up fine.
This is annoying.
Example:
GET http://127.0.0.1:8000/static/js/editor/xyz.js?v=1 404 (NOT FOUND)
but if I visit that URL directly fine. And if I refresh the page a few times, it will work again.
Any ideas?
Chrome 14.0.835.202
Django==1.3
Fabric==1.0.1
Jinja2==2.5.5
PIL==1.1.7
Pygments==1.3.1
South==0.7.3
Sphinx==1.0.5
boto==2.0
chunks==0.1
django-devserver==0.2.1
django-pagination==1.0.7
django-sorting==0.1
django-storages==1.1.3
docutils==0.8
gunicorn==0.12.1
ipython==0.10.1
paramiko==1.7.6
pep8==0.6.1
psycopg2==2.2.2
pycrypto==2.0.1
python-dateutil==1.5
python-memcached==1.45
wsgiref==0.1.2
The dev server is single-threaded so if something keeps waiting, it blocks every request.
I usualy work with the django concurent dev server which is multi-threaded and works much better. Also it is very fast and easy to setup ;)
it might depends on your setup.
what did you do for the static? what's the settings? did you do the collect static?
try this in case
however, about serving static files in development:
Warning This will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably insecure.
This is only intended for local development, and should never be used
in production.
from here
can't you just supply the static files in another server?
After reading all answers if still anyone having this problem then ...
As per Django nature you don't need to do anything to serve static files
just your settings file should have proper configuration as follows:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# **THIS IS USED WHEN YOUR STATIC FILES ARE IN SOME OTHER FOLDER ALSO**
# 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.
FOLDER_NAME,
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
INSTALLED_APPS = (
# other apps
'django.contrib.staticfiles',
)
But if still you face problem put this into your urls.py:
(r'^(path of your file)$', 'django.views.static.serve' , {'document_root': 'PROJECT_ROOT_DIR' + "path to the static folder"}),
Above URL will serve for static files whether they are JS files or CSS or images.
In case of production server you don't need this.
Then run: python manage.py collecstatic.
Hope this helps.