Adding robots txt to your Django project

Django as a CMS may required adding custom files like ads.txt, robots.txt into your Django root project folder and needs to be accessible via – https://example.com/robots.txt

We will show how can you do this within 2 easy steps.

Step 1 –

Drop your robots.txt into the templates extras folder ( can be named anything, we have named it extras here ) and import TemplateView into your project’s urls.py like below.

from django.views.generic import TemplateView

Step 2 –

url(r'^ads.txt$', TemplateView.as_view(template_name='extras/ads.txt')),

That is it, your final code will look like.

from django.views.generic import TemplateView
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^ads.txt$', TemplateView.as_view(template_name='extras/ads.txt')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

We will be happy to hear your thoughts

      Leave a reply

      Techs Tricks
      Logo
      Reset Password