Reputation: 1553
Is there a way to serve 2 or more static directories from Apache? I am using Django and it works fine with 1 static path; when I put 2 it ignores the 2nd.
I don't want to use collecstatic. Just trying to setup my development environment and I like to keep my statics in each app separately. I know in production this will change.
Here is my Apache httpd.conf code:
#games app
Alias /static/ "C:/nerd_project/nerd/games/static/"
<Directory "C:/nerd_project/nerd/games/static">
Require all granted
</Directory>
#ice_cream app
Alias /static/ "C:/nerd_project/nerd/ice_cream/static/"
<Directory "C:/nerd_project/nerd/ice_cream/static">
Require all granted
</Directory>
games app works; the css and images load
ice_cream app does not load and is ignored
Upvotes: 1
Views: 3575
Reputation: 11
I can give a much more easier way to solve this issue that I learned about recently.
If you don't want to serve your static files with Nginx or a apache, but from different apps or folders you can install a pip module called whitenoise
in your virtual environment.
Refer this: whitenoise docs
And then just add this in middlware before the sessionmiddleware in your settings.py file.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # Serve static in production without nginx or apache
'django.contrib.sessions.middleware.SessionMiddleware',
........
]
Then finally you can remove those Alias
statements for rendering static files from your httpd.conf file. Make migrations and restart the server, your problem will be solved.
Upvotes: 1
Reputation: 113
I was also having the same problem till today morning and landed on this page to understand why my static file configs were failing.
You can serve multiple apps with multiple static locations. See how I have two Flask app and one Django app running and static files being served from different locations for all.
ServerName ec2-52-20-211-238.compute-1.amazonaws.com
# logs configuration
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess aira threads=5
WSGIScriptAlias /aira /var/www/aira/aira/aira/aira.wsgi
Alias /aira/static/ /var/www/aira/aira/aira/static/
<Directory /var/www/aira/aira/aira/>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess ama threads=5
WSGIScriptAlias /ama /var/www/AMA/ama.wsgi
Alias /ama/static/ /var/www/AMA/static/
<Directory /var/www/AMA>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess realestatesurvey threads=5
WSGIScriptAlias /RealEstateSurvey /var/www/RealEstateSurvey/mydata/wsgi.py
Alias /RealEstateSurvey/static/ /var/www/RealEstateSurvey/mydata/static/
<Directory /var/www/RealEstateSurvey/mydata/static>
Order allow,deny
Allow from all
</Directory>
For flask template just use For Django: STATIC_URL = '/RealEsatateSurvey/static' in html files:-
Upvotes: 1
Reputation: 1553
I figured out EXACTLY what needs to be done to serve multiple static directories using Apache.
This page https://docs.djangoproject.com/en/1.8/howto/static-files/ mentions this:
"Also this helper function only serves the actual STATIC_ROOT folder; it doesn’t perform static files discovery like django.contrib.staticfiles."
Adding + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
to the urls.py did not load the css and images. You have to read a bit more carefully and go to this page:
I added the following to the my urls.py and now IT WORKS:
from django.conf import settings
from django.contrib.staticfiles import views
if settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', views.serve),
]
I simply commented out the alias and directories pointing to the 2 static directories in Apache's httpd.conf file. Of course this is strictly for a development server only as I like to keep my statics in each app.
Upvotes: 0
Reputation: 2784
You are trying to Alias same URL path /static/
to two directories. That's why it's failing.
Django has pretty fine documention on Serving static files during development.
Check STATICFILES_DIRS settings docs to serve static files from multiple directories.
If you are trying to serve multiple projects static you may consider VirtualHost.
Upvotes: 1