write html-headers (utf-8)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • db

    write html-headers (utf-8)

    Hello all,

    I hope this is the correct newsgroup for this question.

    Does anybody know how I can write a html-header with python(cgi)?
    The problem is, I have a few html templates in which I have a header e.g:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    In this template I write a few Mysql variables.
    Those variable often have german characters. This characters (Gösing in
    stead of Gösing). The german characters in the html template are shown
    correctly.

    If I change the character encoding with the browser to utf-8, all the characters are shown correctly.
    As you can see, I put in the header of the html template that the encoding
    is UTF-8, the browser still shows windows ISO-8859-15. Can I write the
    header with python so the browser uses the utf-8 encoding?

    My hosting providor uses fedora core 2, Python 2.2.3, MySQLdb. Mysql 3.23.58

    I googled for hours, but I can't find the answer. I hope ypu can help me.

    Thanks in advance.

    Arjen

  • Martin v. Löwis

    #2
    Re: write html-headers (utf-8)

    db wrote:[color=blue]
    > In this template I write a few Mysql variables.
    > Those variable often have german characters. This characters (Gösing in
    > stead of Gösing). The german characters in the html template are shown
    > correctly.[/color]

    The problem is then with these variables: apparently, the Mysql
    variables are encoded in iso-8859-15, and you need to recode them
    to UTF-8 first before putting them into the template.

    var = var.decode("iso-8859-15").encode("ut f-8")
    [color=blue]
    > As you can see, I put in the header of the html template that the encoding
    > is UTF-8, the browser still shows windows ISO-8859-15. Can I write the
    > header with python so the browser uses the utf-8 encoding?[/color]

    The problem is not the header, but the body. The browser sees your claim
    that the page is UTF-8, but doesn't believe it. This is because it tries
    to interpret the page as UTF-8, and then finds invalid byte sequences
    (your latin-9 characters), and then knows that the page *can't* be
    UTF-8. It then guesses that the page must be latin-something. That guess
    is wrong, of course, also, because some characters in the page are
    utf-8, and others latin-9. This is invalid HTML.

    Regards,
    Martin

    Comment

    • Benjamin Niemann

      #3
      Re: write html-headers (utf-8)

      db wrote:
      [color=blue]
      > Hello all,
      >
      > I hope this is the correct newsgroup for this question.
      >
      > Does anybody know how I can write a html-header with python(cgi)?
      > The problem is, I have a few html templates in which I have a header e.g:
      >
      > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      > "http://www.w3.org/TR/html4/strict.dtd"> <html>
      > <head>
      > <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      >
      > In this template I write a few Mysql variables.
      > Those variable often have german characters. This characters (Gösing in
      > stead of Gösing). The german characters in the html template are shown
      > correctly.
      >
      > If I change the character encoding with the browser to utf-8, all the
      > characters are shown correctly. As you can see, I put in the header of the
      > html template that the encoding is UTF-8, the browser still shows windows
      > ISO-8859-15. Can I write the header with python so the browser uses the
      > utf-8 encoding?
      >
      > My hosting providor uses fedora core 2, Python 2.2.3, MySQLdb. Mysql
      > 3.23.58
      >
      > I googled for hours, but I can't find the answer. I hope ypu can help me.[/color]
      Does your browser send a Content-Type HTTP header (don't confuse this with
      the HTML <head> part)? If it does and it specifies a charset, this will
      override your <meta http-equiv>. Often iso-8859-1 is the default charset
      for the Content-Type header.

      --
      Benjamin Niemann
      Email: pink at odahoda dot de
      WWW: http://www.odahoda.de/

      Comment

      • Kent Johnson

        #4
        Re: write html-headers (utf-8)

        db wrote:[color=blue]
        > Hello all,
        >
        > I hope this is the correct newsgroup for this question.
        >
        > Does anybody know how I can write a html-header with python(cgi)?
        > The problem is, I have a few html templates in which I have a header e.g:
        >
        > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
        > <html>
        > <head>
        > <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">[/color]

        If you are using CGI you can set the Content-Type header directly. Before you output your HTML do
        print "Content-Type: text/html; charset=UTF-8"
        print # blank line, end of headers

        Kent

        Comment

        Working...