Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a robots.txt on Django

I've seen other answers here, but they aren't really helpful, which is why I am asking. I tried the django-robots framework as well, but it gives me an error when i just put 'robots' in my INSTALLED_APPS

INSTALLED_APPS = [
'index.apps.IndexConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
]
like image 541
reydript Avatar asked Jan 26 '26 03:01

reydript


1 Answers

You can use simply use a TemplateView

template: robots.txt

User-agent: *
Disallow: /admin/
Disallow: /accounts/

urls.py

from django.views.generic import TemplateView

urlpatterns = [
    # ...
    path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type='text/plain')),
    # ...
]
like image 131
anjaneyulubatta505 Avatar answered Jan 28 '26 23:01

anjaneyulubatta505