【问题标题】:Django to AWS: favicon, robots.txt and sitemap?Django 到 AWS:favicon、robots.txt 和站点地图?
【发布时间】:2015-10-23 23:37:58
【问题描述】:

我已使用本教程将 Django 网站部署到 Elastic Beanstalk: https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/

但是,我不知道在哪里以及如何放置 favicon.ico、robots.txt 和 sitemap.xml。有什么想法吗?

【问题讨论】:

    标签: django amazon-web-services amazon-elastic-beanstalk robots.txt favicon


    【解决方案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="...">
    

    不要忘记支持所有设备:full list of favicons

    【讨论】:

    • 我应该把 robots.txt 和 sitemap.xml 放在哪里才能让它工作?
    • 我使用了那些 urlpatterns,但是当我部署我的网站并使用 SEO 检查器网站进行检查时,它找不到 robots.txt 和 sitemap.xml 文件。网站图标效果很好。
    • 是的,我好像能做到
    【解决方案2】:

    对于favicon.icositemap.xml,您可以将它们放在static/ 目录中,并在模板中使用静态URL 引用它们。例如:

    <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 - 你是对的,已移至新 Django 中的TemplateView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    相关资源
    最近更新 更多