将Django迁移到AWS:如何处理favicon、robots.txt和sitemap?

3
2个回答

1
对于 favicon.icositemap.xml,您可以将它们放在 static/ 目录中,并在模板中使用静态网址引用它们。例如:
<link rel="shortcut icon" type="image/png" href="{{STATIC_URL}}/favicon.ico"/>

你的robots.txt可能需要一些额外步骤(就像任何django应用程序一样)。你可以将它放置在templates目录中,并在urls.py中添加以下内容:

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

direct_to_template 似乎已经被弃用。 - Antoni4040
@Antoni4040 - 你是对的,已经迁移到较新版本Django中的 TemplateView - Ewan

1
为了让您的站点地图正常工作,您可以使用 django.contrib.sitemaps 框架:docs。如果您只有一定数量的静态页面,请按照以下方式操作:
urlpatterns = [
    # your robots.txt (and/or humans.txt) file:
    url(r'^robot\.txt$', TemplateView.as_view(
        template_name='txt/robots.txt',
        content_type='text/plain'
    )),
    # your static sitemap:
    url(r'^crossdomain\.xml$', TemplateView.as_view(
        template_name='txt/sitemap.xml',
        content_type='application/xml'
    )),
]

favicon.ico 放在您的 static 文件夹中,并在模板中使用此模板标签:
<link rel="icon" href="{% static 'path/to/favicon.ico' %}" sizes="...">

请不要忘记支持所有设备:完整的网站图标列表

我应该把robots.txt和sitemap.xml放在哪里才能使其正常工作? - Antoni4040
我使用了这些urlpatterns,但是当我部署我的网站并使用SEO检查器网站进行检查时,它找不到robots.txt和sitemap.xml文件。favicon工作得很好。 - Antoni4040
是的,我似乎能够做到。 - Antoni4040

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接