recherche

Maison  >  Questions et réponses  >  le corps du texte

python - django如何添加robots.txt文件?

普通的网站直接把代码放在根目录就可以了
但是感觉django好像很麻烦啊~~,放根目录不行
有什么比较好的办法吗?难道这么简单的东西都要特地写个视图?那岂不是坑爹!?

PHPzPHPz2893 Il y a quelques jours470

répondre à tous(1)je répondrai

  • PHP中文网

    PHP中文网2017-04-17 17:58:49

    Méthode 1

    1. Vous pouvez vous référer à l'emplacement du site officiel de Django : github >

    2. Modifier la configuration de nginx

    location  /robots.txt {
        alias  /path/to/static/robots.txt;
    }

    Méthode 2

    Utilisez la bibliothèque tierce Django-robots

    Méthode 3

    Modifiez

    et mettez urls.py dans le répertoire robots.txt templates

    # urls.py
    from django.views.generic import TemplateView
    
    url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),

    Méthode 4

    Modifier

    sans ajouter de urls.py fichierrobots.txt

    # urls.py
    from django.http import HttpResponse
    
    url(r'^robots\.txt$', lambda r: HttpResponse('User-agent: *\nDisallow: /admin', content_type='text/plain')),

    répondre
    0
  • Annulerrépondre