django staticfiles at url root

▼魔方 西西 提交于 2019-12-21 09:35:31

问题


I have a general question about the new Django 1.3 static file framework.

I really like the new Django staticfile functionality introduced in Django 1.3. Normally, I set STATIC_URL="/static/" and enter the {{ STATIC_URL }} template tag into my templates. It's great how the development server automatically serves up the static files and all of my content is served up as expected.

The {{ STATIC_URL }} would be substituted in the template and might serve up files like this...
example.com/static/css/master.css
example.com/static/images/logo.png
example.com/static/js/site.js

However, I'm working with a legacy site where the static media is mounted at the url root. For example, the path to the static urls might look something like this:

example.com/css/master.css
example.com/images/logo.png
example.com/js/site.js 

It doesn't use the "static" url namespace.

I was wondering if there is a way to get the new staticfile functionality to not use the static namespace and serve the urls above, but still retain the benefits of the new staticfile framework (collectstatic, static files served by development server, etc). I tried setting STATIC_URL="" and STATIC_URL="/", but neither seemed to have the desired effect.

Is there a way to configure static files to serve static files without a namespace? Thanks for your consideration.


回答1:


You can manually add extra locations that do not exist within the static directories within your project:

urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
)

if settings.DEBUG:
    urlpatterns += static('/css/', document_root='app_root/path/to/css/')
    urlpatterns += static('/images/', document_root='app_root/path/to/images/')
    urlpatterns += static('/js/', document_root='app_root/path/to/js/')

This will map the media for the DEBUG dev server. And when you are running your production mode server, you will obviously be handling these static locations from the web server instead of sending the request through to django.




回答2:


why not keep the staticfiles functionality and simply use rewrites at the web server level to serve up the content.

for instance:

rewrite /css /static permanent; (for nginx) 

this would keep your project directory a lot cleaner and also make it easier to move your static directories around in the future, for instance to move to your STATIC_URL to a CDN.




回答3:


This is how you set up your urls.py to serve both index.html and your other static files on / in Django 1.10 (while still being able to serve other Django views):

from django.contrib.staticfiles.views import serve
from django.views.generic import RedirectView

urlpatterns = [

    # / routes to index.html
    url(r'^$', serve,
        kwargs={'path': 'index.html'}),

    # static files (*.css, *.js, *.jpg etc.) served on /
    url(r'^(?!/static/.*)(?P<path>.*\..*)$',
        RedirectView.as_view(url='/static/%(path)s')),
]

See this answer where I wrote a more complete explanation of such a configuration – especially if you want to use it for production.



来源:https://stackoverflow.com/questions/11232922/django-staticfiles-at-url-root

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