Django - loading Robots.txt through generic views

I uploaded the robots.txt file to my templates directory on my production server. I use general views;

from django.views.generic import TemplateView (r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')), 

However, when I load robots.txt in a browser, I get 404 - Page not found.

Can someone tell me what needs to be done to fix this. Thanks.

I must indicate that in a local environment this works.

+6
source share
1 answer

I finally get it. I had to add '/' to ^ robots.txt $

 (r'^robots\.txt/$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')), 

This is elementary! I assumed that by default APPEND_SLASH is true, however on a production server this did not work.

Let me know if anyone can provide some information about this.

+13
source

Source: https://habr.com/ru/post/980477/


All Articles