Static files not being served from seperate django staticfile application

佐手、 提交于 2019-12-12 01:08:18

问题


I'm using webfaction to make a simple static website. I think I have done everything correctly, but still my static files (i.e css, images) are not being served. I used this guide to set things up:

http://docs.webfaction.com/software/django/getting-started.html

I did everything they told me to, so in brief:

1) made a separate static media application

2) updated settings.py

3) ran

python manage.py collectstatic 

All of the above went pretty much according to plan and I can now see my static files in the staticfile application that I named personalsitestatic. My django application is called personalsite.

The directory structure looks like this:

  • webapps
    • htdocs
    • personalsite
      • apache2
      • bin
      • lib
      • PersonalWebsite
        • manage.py
        • PersonalWebsite
          • settings.py, urls.py, ect
        • main
          • settings.py, urls.py, static files directory, ect
    • personalstiestatic
      • bootstrap
      • custom

This is what the relevant values in my settings.py looks like:

STATIC_ROOT = '/home/<myname>/webapps/personalsitestatic/'
STATIC_URL = 'http://<sitename>.com/static/'
TEMPLATE_DIRS = (
"/home/<myname>/webapps/personalsite/PersonalWebsite/templates/",)

I don't have any additional static directories outside of main/static so I am not using STATIC_DIRS. The templates appear to be loading fine, just none of the static files. When I inspect with firefox's debugger/inspect element thing it just says "stylesheet could not be loaded". Can anyone help me out here or at least give me some suggestions for debugging?


回答1:


I was being silly and did not mount it to the /static url, hope this helps someone!




回答2:


I added those two lines to my httpd.conf, after some research this seems to be the safest and best performing way to serve static contents with Apache.

Alias /media/ /home/USERNAME/webapps/APPNAME/media/
Alias /static/ /home/USERNAME/webapps/APPNAME/staticfiles/

Notice i kept the static and media folders outside the "myproject" folder (the one containing py files) since I have a hook in the git repo self deploying the app, so i don't want those files to be in the way.



来源:https://stackoverflow.com/questions/21170115/static-files-not-being-served-from-seperate-django-staticfile-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!