Django generating strange code

Asked

Viewed 139 times

5

I am using the visual studio community and created a web project with Django, but when making the homepage, is generating me this code:

inserir a descrição da imagem aqui

And the code I have is this:

{% load staticfiles %}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>{{ title }}</title>

        <link rel="stylesheet" type="text/css" href="{% static 'app/content/materialize.css' %}" />
        <link rel="stylesheet" type="text/css" href="{% static 'app/content/app.css' %}" />
        <link rel="stylesheet" type="text/css"  href="https://fonts.googleapis.com/icon?family=Material+Icons" />
    </head>
    <body>

Does anyone know why while running, he does it? I trash html?

this is creating a space on my page that should not have.

code that renders:

def home(request):
    """Renders the home page."""
    assert isinstance(request, HttpRequest)
    return render(
        request,
        'app/index.html',
        {
            'title':'Home Page',
            'year':datetime.now().year,
        }
    )
  • Checks if your html document (the template file) has any null characters (with Regex in an advanced text editor you can find by typing \0), can be this. Also check if you saved all documents in UTF8.

  • @Guilhermenascimento, the documents are in utf-8 and have no null character, I tried to leave the document totally blank with nothing, nor the basic html tag and the same thing...

  • Are you sure the template being rendered is yours? I don’t know how Visual Studio does it, but maybe it’s rendering a template of its own and including its like contents page (i.e. putting everything in the body of the "parent" template). What is the code that renders and returns this template? (i.e. the view)

  • 1

    @mgibsonbr, I added in the question.. I find it strange because the model that comes in visual studio works right, it doesn’t break html, and I just changed the content of the home page and start to give these crazy...

  • You better give CTRL+U... The new Chrome update shows some strange stops on Console

  • Did you use Windows Notepad to make this issue? (see that answer in Soen, ask me if you want more details) By the way, there is a difference between the HTML actually received from the server (which you see by the "view source code" function of the browser) and the DOM generated from it (which you see by the "inspect element" or similar function). Your screenshot seems to me the second case. See what is actually being generated, if the strange content is coming within the body (doubt, but may be) or at the beginning of the file (I think most likely).

  • @mgibsonbr, that’s right, I changed the codiffication to utf-8 without BOM and it works... now it’s doubtful.. will I have to do it for the next files I’ll create?

  • 1

    @Meuchapeu I’m afraid so... Unless you use some other text editor that you use (or can be set to) by default UTF-8 without BOM.

Show 3 more comments

1 answer

2


The code &#65279; matches the Unicode character U+FEFF, widely used in UTF-16 encodings as "byte checkmark" (GOOD). However, some text editors (like Windows Notepad) put this BOM even in UTF-8 files, which do not need BOM (since the order of bytes is always the same). This is probably done with the intention of "marking" the file as UTF-8, although this practice is discouraged.

As its modified file had this character at the beginning of the file, it was sent to the browser along with the rest, and the rules of Parsing of the browser determined that it should be part of the content of it. By the robustness/fault tolerance of the usual algorithms of Parsing HTML, the fact that it does not have a html and body does not prevent them from accepting the content - placing them for you. Thus, the resulting DOM ended with a head empty and everything that was in your template inside the body automatically created.

The ideal way to fix this is by taking BOM out of your files. Alternatively, maybe it is possible to make the browser itself ignore this GOOD by informing it on content_type that the file is encoded as "UTF-8 with GOOD" (which if I’m not mistaken is UTF-8Y):

resposta = render(
    request,
    'app/index.html',
    {
        'title':'Home Page',
        'year':datetime.now().year,
    }
)
resposta.charset = "UTF-8Y"
return resposta

(I have no way to test at this time if the above solution will be accepted by the main browsers, so I suggest using them only as a last option)

Sources: those three questions in the Soen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.