开发者

Where to put static files that should be served directly under the server root?

I just migrated an old Django project to make use of the staticfiles app. Before that i had all needed files in a directory called static that got served directly under the server root. This directory is now served under STATIC_URL which is fine, except for the files that should be served directly under the server root.

I know how to serve files directly from root (like /favicon.ic开发者_Go百科o or /robots.txt) but where should i put those? If i put them anywhere beneath STATIC_ROOT they will be served by two URLs (e.g. /file.txt and /static/foobar/file.txt) which is not good practice.

Any ideas?


I've resolved both problems (favicon.ico, robots.txt) within url.py but with some differences. I don't like the solution one would firstly think to make a view an perform a render_to_response.

EDIT: From Django 1.5 direct_to_template and redirect_to were deprecated, so now you can use class-bassed views.

For Django 1.5:

For robots.txt, add the following line to your urlpatterns:

from django.views.generic.base import RedirectView, TemplateView

(r'^robots\.txt$', TemplateView.as_view(template_name="robots.txt",
                                        content_type='text/plain')),

I use the generic class view TemplateView, and specify the template to use (robots.txt that should be in your template directory), and the mimetype.

For favicon.ico, add the following line to your urlpatterns:

(r'^favicon\.ico$', RedirectView.as_view(
                            url=settings.STATIC_URL + 'img/favicon.ico')),

This redirects /favicon.ico to STATIC_URL + img/favicon.ico (eg: /static/img/favicon.ico) favicon.ico shall be in your static directory.

These approaches could be used for any media or html content.

For previous versions of Django you could use:

(r'^robots\.txt$', direct_to_template, {'template': 'robots.txt',
'mimetype': 'text/plain'}),

(r'^favicon\.ico$', redirect_to, 
{'url': settings.STATIC_URL + 'img/favicon.ico'}),


Keep them in static and have your webserver redirect /static/favicon.ico to /favicon.ico.

To answer more completely:

If you have the file favicon.ico, this is a static file and as such should exist inside of STATIC_ROOT. However this file is an exception to the normal rule and you do not want it to exist at /static/favicon.ico, you want it to exist at /favicon.ico. Since this is an exception to the rule, you add in a special rule just for this file to your webserver configuration so that it is also served at /favicon.ico.

Now you have the same resource served by 2 different urls which is a bad thing. Since you went out of your way to add the rule to make your file served at /, We'll assume that this is the canonical url and tell the webserver to redirect /static/favicon.ico to /favicon.ico. Now you have the same resource, served from one location.

Other files in the root of /static/ will not be affected by this, because in the rules you setup in the webserver for favicon.ico, you specified favicon.ico because of the exceptional nature of this file (and any other file you want to serve from /).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜