Django Static Files CSS

爷,独闯天下 提交于 2021-02-10 15:53:19

问题


How can I view my static css files? I've set my STATIC_ROOT, and am using python manage.py runserver.

In my development environment, according the docs, I only need to place my static files (in this case, /static/css/typography.css) in my STATIC_ROOT, and python manage.py runserver will automatic create the views necessary to access it if I have DEBUG = True.

STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static")

I've also tried manually adding the views in URLConf, which won't display the css file either:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# ... the rest of your URLconf goes here ...

urlpatterns += staticfiles_urlpatterns()

In my template, the {{ STATIC_URL }} gets to the correct address (/static/css/typography.css), but it will not serve the file when I try to access it:

   <link href="{{ STATIC_URL }}css/typography.css" rel="stylesheet" type="text/css">

Notes: The other Django related static files questions on StackOverflow are over two years old. Django version 1.3b1 differentiates STATIC (static files, such as css and images) and MEDIA (user-uploaded file).


回答1:


Besides. all the tries mentioned above, you must also make sure that your template is receiving the RequestContext when called from the view.

http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/

This link gives various ways of doing the same and you could choose any one. :) Also, the TEMPLATE_CONTEXT_PROCESSORS must be added to settings.py for this to take effect.

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "myapp.processor.foos",
)

Note: STATIC_ROOT is the place where all your static files are stored by Django after collecting them from STATICFILES_DIRS. Runserver will pick them up from the path mentioned in STATIC_ROOT, so STATIC_URL should point to the same location as STATIC_ROOT.




回答2:


Answered here: Django staticfiles app help

I was putting my files in STATIC_ROOT, so adding this works:

STATICFILES_DIRS = (STATIC_ROOT,)



回答3:


In development:

#settings.py have this by default 

STATIC_URL = '/static/'

This means when you want to use static files, django will look for them in folder 'static' in your app directory. So you need to create folder 'static' in every your app and put inside your static files.

In production:
1. Make place to hold static files from all app's

STATIC_ROOT = "/var/www/example.com/static/"

2. Run python manage.py collectstatic to collect static files from all app's in STATIC_ROOT folder.
3. In settings.py change DEBUG=False and ALLOWED_HOSTS = ['yourhostadress'].
4. Point your webserver to STATIC_ROOT folder (eg. for Apache2.4):

Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>

After this setup, your django project should be able to run (using mod_wsgi) and use static files on Apache web server.



来源:https://stackoverflow.com/questions/4848102/django-static-files-css

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