-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not available in Django4 #142
Comments
The error you are seeing when trying to set the cache time with django-robots in Django version 4 is likely caused by a change in the TemplateResponse object which no longer has a headers attribute that django-robots is trying to access. One way to work around this issue is to manually set the cache headers in a custom middleware for your Django project. Here's an example implementation that you can adapt to your own project:
This middleware will check if the requested path is /robots.txt and then either set the cache headers with a timeout of 600 seconds (10 minutes) or return a HttpResponseNotModified object if the response has not been modified since the last request. You can adjust the cache timeout to the desired value for your use case. To use this middleware, add it to the MIDDLEWARE setting in your Django settings file: MIDDLEWARE = [ Make sure to replace path.to with the actual path to your middleware class. With this middleware in place, django-robots should be able to set the cache time for the robots.txt file properly in Django version 4. The issue you're experiencing with the django-robots package in Django version 4 is due to some changes in the Django framework. The TemplateResponse object no longer has a headers attribute. Instead, you will use the ._headers attribute, which is a dictionary that holds the headers for the response. Find the place in the code where the .headers attribute is being accessed and change it to ._headers. After making the change, test the package to ensure it's working as expected. The error you're seeing in Django 4 when setting the cache time for django-robots is due to changes in the way TemplateResponse objects are handled in Django 4. To fix this error, you can modify the RobotsView class in django_robots/views.py to use a HttpResponse object instead of a TemplateResponse object:
In this modified implementation, we're using a HttpResponse object instead of a TemplateResponse object, and setting the Cache-Control header directly on the response object. This should resolve the AttributeError you're seeing. |
In Django 4, an error occurs when the cache time is set
AttributeError: 'TemplateResponse' object has no attribute 'headers'
The text was updated successfully, but these errors were encountered: