How do I go straight to template, in Django's urls.py?

后端 未结 2 934
鱼传尺愫
鱼传尺愫 2020-12-24 00:15

Instead of going to views.py, I want it to go to to a template, robots.txt.

2条回答
  •  不思量自难忘°
    2020-12-24 00:38

    A further update for more recent versions and including mime type from this site:

    http://www.techstricks.com/adding-robots-txt-to-your-django-project/

    from django.conf.urls import url
    from django.views.generic import TemplateView
    
    urlpatterns = [
        #... your project urls
        url(r'^robots.txt$', TemplateView.as_view(template_name="robots.txt", content_type="text/plain"), name="robots_file")
    ]
    

提交回复
热议问题