From 82ada94bd756ced2069d31e783ad8163ac21b522 Mon Sep 17 00:00:00 2001 From: Ivan Mamtsev Date: Mon, 21 Oct 2024 18:12:31 +0400 Subject: [PATCH] fix robots view --- config/urls.py | 4 ++-- contributors/views/robots.py | 16 ++++++++++++++++ {static => templates}/robots.txt | 0 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 contributors/views/robots.py rename {static => templates}/robots.txt (100%) diff --git a/config/urls.py b/config/urls.py index b50c187..4a8f06f 100644 --- a/config/urls.py +++ b/config/urls.py @@ -1,11 +1,11 @@ from django.conf import settings from django.contrib.sitemaps.views import sitemap -from django.contrib.staticfiles.views import serve from django.urls import include, path from django.views.i18n import JavaScriptCatalog from contributors.admin.custom import site from contributors.sitemap import sitemaps +from contributors.views import robots def trigger_error(request): @@ -21,7 +21,7 @@ def trigger_error(request): path('auth/', include('auth.urls')), path('', include('contributors.urls')), path('sentry-debug/', trigger_error), - path('robots.txt', serve, {'path': 'robots.txt'}), + path('robots.txt', robots.RobotsTxtView.as_view(), name='robots_txt'), path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'), ] diff --git a/contributors/views/robots.py b/contributors/views/robots.py new file mode 100644 index 0000000..53458d5 --- /dev/null +++ b/contributors/views/robots.py @@ -0,0 +1,16 @@ +from django.http import HttpResponse +from django.template import loader +from django.views.generic import TemplateView + + +class RobotsTxtView(TemplateView): + template_name = "robots.txt" + content_type = "text/plain" + + def render_to_response(self, context, **response_kwargs): + template = loader.get_template(self.template_name) + response = HttpResponse( + template.render(context, self.request), + content_type=self.content_type + ) + return response diff --git a/static/robots.txt b/templates/robots.txt similarity index 100% rename from static/robots.txt rename to templates/robots.txt