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

Firefox Character Encoding #100

Open
9mido opened this issue Apr 14, 2020 · 1 comment
Open

Firefox Character Encoding #100

9mido opened this issue Apr 14, 2020 · 1 comment

Comments

@9mido
Copy link

9mido commented Apr 14, 2020

Firefox console shows this error:

"The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature."

Current code:

url(r'^robots.txt/', include('robots.urls')),


Alternatives?

path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type='text/plain')),
url(r'^robots.txt', lambda x: HttpResponse("User-Agent: *\nDisallow:", content_type="text/plain"), name="robots_file"),


How do you combine include('robots.urls') with the alternatives or is it not possible?

It should probably be:

content="text/html; charset=utf-8" or "text/plain; charset=UTF8" not 'text/plain'​​

curl -s -D - 127.0.0.1:8000/robots.txt/
HTTP/1.1 200 OK
Date: Tue, 14 Apr 2020 01:05:52 GMT
Server: WSGIServer/0.2 CPython/3.7.3
Content-Type: text/plain

@some1ataplace
Copy link

The error you're encountering in Firefox is due to the character encoding not being specified for the plain text document. To resolve this issue, you can set the content_type attribute with the charset specified in the HttpResponse or TemplateView.

Since you're using django-robots, you can simply extend the base view class to include the charset in the content_type. Here's a possible solution:

  1. Create a new file named custom_robots.py in your app folder and add the following code:
from django.http import HttpResponse
from robots.views import RulesView

class CustomRulesView(RulesView):
    def render_to_response(self, context, **response_kwargs):
        response_kwargs.setdefault('content_type', 'text/plain; charset=UTF8')
        return HttpResponse(self.get_template(context), **response_kwargs)
  1. In your urls.py, import the CustomRulesView class and update your urlpatterns:
from django.urls import path, re_path
from .custom_robots import CustomRulesView

urlpatterns = [
    re_path(r'^robots.txt$', CustomRulesView.as_view(), name='robots_file'),
]

This should now properly set the charset for your robots.txt and resolve the error. Please let me know if you need further assistance or explanation.


To fix the character encoding issue in Firefox when using django-robots, you can modify the code for serving the robots.txt file to include the appropriate character encoding.

One option is to use the TemplateView class from Django's views.generic module to render the contents of the robots.txt file as a template. You can set the content_type argument to the appropriate value based on the character encoding used.

Here's an example implementation:

from django.views.generic import TemplateView

urlpatterns = [
    path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type='text/plain; charset=utf-8')),
]

Alternatively, you can use a lambda function to serve the robots.txt file. You can set the charset attribute of the HttpResponse object to the appropriate value based on the character encoding used.

Here's an example implementation:

from django.http import HttpResponse

def serve_robots_txt(request):
    content = render_to_string('robots.txt', {'domain': get_current_site(request)})
    response = HttpResponse(content_type='text/plain')
    response.charset = 'utf-8'
    response.write(content)
    return response

urlpatterns = [
    path('robots.txt', serve_robots_txt),
]

This code will render the robots.txt template and return an HttpResponse object with the appropriate character encoding set to UTF-8. This should resolve the character encoding issue in Firefox when using django-robots.

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

2 participants