Context
I am trying to setup a django continuous integration developpement system on a apache2 server via wsgi and Mercurial.
Setup seems to work fine, but with the changegroup hook, I run python manage.py collectstatic --settings="sitename.settings" --noinput to update the static files directory located in /path/to/sitename/static/.
Problem
But with each run of the python manage.py collectstatic --settings="sitename.settings" --noinput command, my /path/to/sitename/static/ directory gets a complete copy of the filesystem in /path/to/sitename/, thus leading in easy access to all code files, settings files, etc... with 5 run, i have 5 nested /static/ in the static directory, eg. static/, static/static/, static/static/static/ and so on...
This will become a real problem. I could just remove everything inside static/ with each run, but some file permissions need to be preserved.
Configuration used
At the moment, DEBUG = True
/path/to/sitename/settings.py:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/path/to/sitename/static/'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# Additional locations of static files
STATICFILES_DIRS = (
# 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.
'/path/to/sitename/blog/static',
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
Question
What am I missing? Is there something in the use of staticfile i did not understand?
Will I have other problems with this kind of solution?
STATIC_ROOT is where collectstatic will dump the files, and STATICFILES_DIRS is where it will search for static files, note that this is optional. By default django will search for a static directory in all registered apps.
As far as duplication, as per the docs:
Duplicate file names are by default resolved in a similar way to how
template resolution works: the file that is first found in one of the
specified locations will be used. If you're confused, the findstatic
command can help show you which files are found.
I didn't see where STATIC_ROOT is set in your settings, so I suspect you need to make sure these two are pointing at the right place.
The easiest way to use static files is to create a directory called static as a subdirectory of your application directory and leave STATICFILES_DIRS empty.
Related
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!
I've been running into a lot of issues trying to use static files and getting them to work with the development server.
I've been using the link: https://docs.djangoproject.com/en/1.4/howto/static-files/#staticfiles-development
When I run the development server, I notice that my CSS files are not getting pulled in but they are specified with the correct directory implying that Django is not finding my static files folder. My folder is defined in the project directory fold as 'static' and contains a folder called 'css' with 'bootstrap.css'.
When I look at the page source, I see:
<link href="css/bootstrap.css" rel="stylesheet">
but I can't view the CSS implying a problem with finding the correct directory.
What I added in settings was
STATICFILES_DIRS = (
'static',
# 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 dir structure: mysite/static/css/bootstrap.css
Thanks for any help :D
EDIT: Also if I can put non-existent directories into STATICFILES_DIR in settings.py without triggering an error in debug mode; does that imply that I have not configured Django to even look for static directories?
EDIT: I've also added a RequestContext to my views.py but it did not work out.
1: Make sure settings.DEBUG is True
2: Give absolute path to directory that contains static media. In this case, /PATH/TO/mysite/static/
3: Make sure calls to static files actually point to settings.STATIC_URL
Your example points to relative url css/bootstrap.css. It should probably point to {{ STATIC_URL }}css/bootstrap.css unless you got lucky and your page is routed at a URL that matches your STATIC_URL exactly.
With condition 1 and 2 met, static media is served AT settings.STATIC_URL FROM settings.STATICFILES_DIRS and other staticfiles finders.
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.
The more I learn Django, the more I discover new things.
I read the official docs, stackoverflow and google but I still have doubts.
What's the correct/normal way to organize our static files?
I mean folders and settings.py
I have something like:
CURRENT_PATH = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(CURRENT_PATH, 'static')
STATIC_URL = '/static/'
Ok, Im going to collect all my apps statics on ~/static/
I created a static/appname folder on every app and I put all my app's static there.
Also, I need a static folder to project-wide statics (what's the common name for it? Since I used /static/ for collected stuff and they cannot be equal).
So far so good, I have css like:
href="{{ STATIC_URL }}appname/styles.css"
and it works like charm.
But I think that when I deploy my app, I have to run 'collectstatic' so I put that '/static/' folder serving on Cherokee.
The question is... will that work? I tried commenting the AppDirectoryFinder and the _DIRS one and that doesn't work on local (Having the static stuff collected, I mean, the css on /static/ and in the other folders too).
Is just better to have one static folder on root for all the project? And copy the admin css to that folder (AKA manually collectstatic).
The projects I see on github/bitbucket are ready to be deployed, I need to know the steps to go from dev to deploy.
Thanks!
I'll break this down as I use the django static app
url at which your static media will be served
STATIC_URL = '/static/'
this is used for 2 things
{{STATIC_URL}} in your templates and the static file url
and for hosting your static files in django (DEVELOPMENT ONLY)
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
The location at which your files reside on the server
STATIC_ROOT = '/var/www/website/static'
this is used when you run collectstatic and is where your webserver should be looking
your file finder definition
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
I've used the default from django you can of course use more but here is the crux of what you are looking to know
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
will find any folder named "static" that is inside an installed app
'django.contrib.staticfiles.finders.FileSystemFinder'
will tell django to look at STATICFILES_DIRS (this is your project wide static files)
which should be defined as a tuple
STATICFILES_DIRS = (
join( CURRENT_PATH, 'static' ),
)
where 'static' is whatever you want and you can add in as many other folders to monitor as you wish.
the sub directories you place inside each app ie:app/static/appname are not necessary but will ensure that files of the same name inside different apps don't overwrite files from other apps or your root static folders
all of this was taken from my own experience and https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/
Also, I need a static folder to project-wide statics (what's the common name for it? Since I used /static/ for collected stuff and they cannot be equal).
Are you sure? I'm pretty sure I'm using the same folder to collect my static files and to hold my project-wide static files. Not sure if that's not recommended pracice, but it works for me.
Note that this is just on the deployment side; my codebase just has the project static files.
I am developing a Django (1.3) project composed of many applications. Each application has its own static files under its own static directory. Moreover, I have added a directory called project_static which should contain static files which are common throughout the various applications, such as jQuery.
The problem I immediately run into is that of naming collisions. By default, collectstatic will just put everything under a global static directory, without classifying them by application. This does not work for me, as each application has - for instance - a file called css/screen.css.
The way I solved it is by removing django.contrib.staticfiles.finders.AppDirectoriesFinder from STATICFILES_FINDERS and using namespaced static files dirs, so my settings now look like:
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'project_static'),
('my_app', os.path.join(PROJECT_PATH, 'my_app', 'static')),
('another_app', os.path.join(PROJECT_PATH, 'another_app', 'static')),
...
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
The problem is that in this way I lose the static files for all other applications (e.g. django.contrib.admin). Of course I could manually add the admin, but I'm not sure how to do this without breaking the admin, which has a different directory structure.
Is there a better way to manage the static files with more than one application?
This is the same problem that occurs with using app-specific templates directories. If you just throw the files directly under templates in the app, you'll end up with name collisions if two apps or even the project-level templates directory utilize templates of the same name. The fix for that is to actually put the templates in a directory of the name of app inside the templates directory like:
- some_app
- templates
- some_app
- index.html
So, I apply the same idea to static:
- some_app
- static
- some_app
- css
- img
- js
That way, when you run collectstatic, each individual app's static files get placed inside a namespaced directory. The only change you need to make is to prefix each of the files with the app-name directory in your templates. So instead of {{ STATIC_URL }}css/style.css you have {{ STATIC_URL }}my_app/css/style.css.