开发者

Django Static Files CSS

开发者 https://www.devze.com 2023-02-08 03:10 出处:网络
How can I view my static css files?I\'ve set my STATIC_ROOT, and am using python manage.py runserver.

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开发者_如何学Go old. Django version 1.3b1 differentiates STATIC (static files, such as css and images) and MEDIA (user-uploaded file).


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.


Answered here: Django staticfiles app help

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

STATICFILES_DIRS = (STATIC_ROOT,)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消