You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's an updated code for django-robots that allows the Django sites package to be optional:
from django.conf.urls import url
from django.http import HttpResponse
from django.urls import reverse
from django.views.generic.base import View
from django.contrib.sites.shortcuts import RequestSite, get_current_site
class RobotsView(View):
def get(self, request, *args, **kwargs):
content = "User-agent: *\nDisallow:\n"
protocol = request.scheme
site = get_current_site(request) if 'django.contrib.sites' in settings.INSTALLED_APPS else RequestSite(request)
domain = site.domain
sitemap_url = '{}://{}{}'.format(protocol, domain, reverse('sitemap'))
content += 'Sitemap: {}\n'.format(sitemap_url)
response = HttpResponse(content, content_type='text/plain')
return response
urlpatterns = [
# ... your other URLs ...
url(r'^robots\.txt$', RobotsView.as_view(), name='robots'),
]
Changes Made:
Updated get_current_site() method to use RequestSite instead of django.contrib.sites.shortcuts.get_current_site when django.contrib.sites is not in INSTALLED_APPS
Added the protocol to the sitemap URL to make it a full URL using the request.scheme attribute
Moved the sitemap URL into the content variable and appended it to the User-agent and Disallow directives using the Sitemap directive
Note that you will need to add the 'django.middleware.locale.LocaleMiddleware', middleware to your settings file to enable multilanguage support in the robots.txt file.
It would be a lovely addition if there was no longer a need for the Django Sites package.
The current Sitemaps package has a brilliant fallback:
I think you might be able to replace
django-robots/src/robots/views.py
Line 21 in be2e781
The text was updated successfully, but these errors were encountered: