Reputation: 2926
Running Django 1.8.4
Static files have been working on local. I've uploaded the project to a VPS and everything is working except the static files.
Settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
Example CSS files reference in template
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet">
sudo nano /etc/nginx/sites-available/soundshelter
server {
server_name MYSERVER;
access_log off;
location /static/ {
alias /opt/soundshelter/soundshelter/static/; #this is the valid location
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
Urls.py
from django.conf.urls import patterns, url,include
from soundshelterapp import views
urlpatterns = patterns('soundshelterapp.views',
url(r'^$', views.home, name='home'),
url(r'^release/(?P<release_id>\d+)$', views.release, name='release'),
url(r'^genre/(.*)$', views.genre, name='genre'),
url(r'^label/(.*)$', views.label, name='label_no_country'),
url(r'^artist/(.*)$', views.artist, name='all_artists'),
url(r'^recommendations/(.*)$', views.recommendations, name='user'),
url(r'^personalised$', views.personalised, name='name'),
url(r'^social/', include('social.apps.django_app.urls', namespace='social')),
url(r'^login/$', 'login',name='login'),
url(r'^logout/$', 'logout',name='logout'),
url(r'^save_release/$', views.save_release, name='save_release'),
url(r'^unsave_release/$', views.unsave_release, name ='unsave_release'),
)
Upvotes: 0
Views: 979
Reputation: 379
Do render a static file in your live website, you have to add the below code in your setting file.
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Static files (CSS, JavaScript, Images)
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collect static to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
Also add this in your INSTALLED APPS,
INSTALLED_APPS =
['django.contrib.staticfiles',]
Upvotes: 0
Reputation: 1634
To avoid hardcoding, you can set STATIC_ROOT this way:
STATIC_ROOT = os.path.join (os.path.dirname(BASE_DIR), "staticfiles", "static")
So your static files for production will be in a directory near your project's folder. Moreover you can do the same stuff with MEDIA_ROOT.
MEDIA_ROOT = os.path.join (os.path.dirname(BASE_DIR), "staticfiles", "media")
Upvotes: 1
Reputation: 2926
Thanks Hedde van der Heide for the solution
Setting STATIC_ROOT to the actual location of the files worked
e.g.
STATIC_ROOT = "/var/www/example.com/static/"
https://docs.djangoproject.com/en/1.8/howto/static-files/#deployment
Upvotes: 0