django-robots icon indicating copy to clipboard operation
django-robots copied to clipboard

Allow the Django Sites package to be optional

Open jensenbox opened this issue 3 years ago • 2 comments

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 https://github.com/jazzband/django-robots/blob/be2e781b46aeceb728303f46da585d237e079409/src/robots/views.py#L21 and just use what Django provides: https://github.com/django/django/blob/main/django/contrib/sites/shortcuts.py#L18

jensenbox avatar May 04 '22 07:05 jensenbox

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.

some1ataplace avatar Mar 27 '23 21:03 some1ataplace

@some1ataplace Are you open to making a PR?

tony avatar Jul 05 '23 10:07 tony