Skip to content
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

Allow the Django Sites package to be optional #130

Open
jensenbox opened this issue May 4, 2022 · 2 comments
Open

Allow the Django Sites package to be optional #130

jensenbox opened this issue May 4, 2022 · 2 comments

Comments

@jensenbox
Copy link

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

def get_current_site(self, request):
and just use what Django provides: https://github.com/django/django/blob/main/django/contrib/sites/shortcuts.py#L18

@some1ataplace
Copy link

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.

@tony
Copy link
Member

tony commented Jul 5, 2023

@some1ataplace Are you open to making a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants