ads.txt or robots.txt in Django project

Not so long ago publishers are required to place ads.txt file in your projects. I won't go far on why this file is needed. Will this file also fix this problem or not.. That's not mine either, I'll just show you how can you use simple templateview to create an ads.txt or a robots.txt.

So sometimes you need to have a view that does nothing but load a template and print it in a HTTP / HTTPS respose. You can do it with.

TemplateView



vim immobe/urls.py

from django.views.generic import TemplateView


url(r'^ads\.txt$', TemplateView.as_view(template_name="ads.txt")),

vim templates/ads.txt

google.com, pub-420942487343455, DIRECT, f08c47fec094DFDF


I'll just say that ads can appear on 1 website from multiple publishers and that makes it hard to understand if these ads were posted here with owner's permission or not.




More information about class based views

https://docs.djangoproject.com/en/4.0/topics/class-based-views/

Comments