Encoding UTF 8 (añ,etc) and datetime function

206 views
Skip to first unread message

David Pineda

unread,
Jan 14, 2014, 11:04:19 AM1/14/14
to django...@googlegroups.com
Hello, i'm beginning to learn python+django and i'm doing that handbook https://github.com/jacobian/djangobook.com/blob/master/chapter03.rst
In the example whit datetime i have a problem when i active the utf-8 encondig because i use á,ñ, etc (i'm chilean and we talk in spanish)
So, when i deactive it's works fine, but in the future when i do something more i will need the utf8 enconding and all functionalities like datetime working and i don't know  yet how to do to make work together that,
Thanks for help me :)

Erik Cederstrand

unread,
Jan 14, 2014, 11:21:20 AM1/14/14
to Django Users
Please post the code where you are trying to use non-ASCII characters. What did you try, and what is the exact error message you see?

Thanks,
Erik

David Pineda

unread,
Jan 14, 2014, 1:12:09 PM1/14/14
to django...@googlegroups.com
The code:

from django.http import Http404, HttpResponse
import datetime
# coding: utf-8

def hello(request):
    return HttpResponse("Hello world")
def home_page(request):
    return HttpResponse("Página de Inicio")
def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)
def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
    return HttpResponse(html)

Whit utf8

SyntaxError at /time/plus/99/

Non-ASCII character '\xc3' in file /home/david/Documents/Django/tutorial2/base1/base1/views.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 8)
Request Method: GET
Request URL: http://127.0.0.1:8000/time/plus/99/
Django Version: 1.5.5
Exception Type: SyntaxError
Exception Value:
Non-ASCII character '\xc3' in file /home/david/Documents/Django/tutorial2/base1/base1/views.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 8)
Exception Location: /home/david/Documents/Django/tutorial2/base1/base1/urls.py in <module>, line 2
Python Executable: /usr/bin/python
Python Version: 2.7.5
Python Path:
['/home/david/Documents/Django/tutorial2/base1',
 '/usr/lib/python27.zip',
 '/usr/lib64/python2.7',
 '/usr/lib64/python2.7/plat-linux2',
 '/usr/lib64/python2.7/lib-tk',
 '/usr/lib64/python2.7/lib-old',
 '/usr/lib64/python2.7/lib-dynload',
 '/usr/lib64/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/PIL',
 '/usr/local/lib64/python2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode']
Server time: Tue, 14 Jan 2014 12:12:55 -0600

The code whituot 'á'-->a

from django.http import Http404, HttpResponse
import datetime
# coding: utf-8

def hello(request):
    return HttpResponse("Hello world")
def home_page(request):
    return HttpResponse("Pagina de Inicio")
..
.
.
It's work

Sergio Garcia

unread,
Jan 14, 2014, 1:58:40 PM1/14/14
to django...@googlegroups.com
I had the some problem (Português tem seus acentos :) )

From the Python reference:
http://www.python.org/dev/peps/pep-0263/
    Python will default to ASCII as standard encoding if no other
    encoding hints are given.

    To define a source code encoding, a magic comment must
    be placed into the source files either as first or second
    line in the file, such as:

          # coding=<encoding name>

    or (using formats recognized by popular editors)

          #!/usr/bin/python
          # -*- coding: <encoding name> -*-

    or

          #!/usr/bin/python
          # vim: set fileencoding=<encoding name> :




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/67d88b83-2be9-4295-93f5-c285f760736c%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Sergio Garcia

unread,
Jan 14, 2014, 2:01:19 PM1/14/14
to django...@googlegroups.com
Basically a "# coding=utf-8" at begining of your file and saving it with utf-8 will solve your problem.

Tom Evans

unread,
Jan 15, 2014, 4:55:55 AM1/15/14
to django...@googlegroups.com
On Tue, Jan 14, 2014 at 6:12 PM, David Pineda <dah...@gmail.com> wrote:
>
> The code:
>
> from django.http import Http404, HttpResponse
> import datetime
> # coding: utf-8
>
> def hello(request):
> return HttpResponse("Hello world")
> def home_page(request):
> return HttpResponse("Página de Inicio")
> def current_datetime(request):
> now = datetime.datetime.now()
> html = "<html><body>It is now %s.</body></html>" % now
> return HttpResponse(html)
> def hours_ahead(request, offset):
> try:
> offset = int(offset)
> except ValueError:
> raise Http404()
> dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
> html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
> return HttpResponse(html)

Python needs to be told that a string can hold unicode by declaring it
as a unicode string. Then, you can put anything you like in to it. You
do this by putting a u before the opening quote of the string.

Eg, this is wrong, as it does not use a unicode string

def home_page(request):
return HttpResponse("Página de Inicio")

This would be the correct version:

def home_page(request):
return HttpResponse(u"Página de Inicio")

Do this for all strings in your project where they might contain
unicode characters, eg so in this code the strings "html" should be
marked as unicode, so should both the raw strings being passed to the
HttpResponse constructor.

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages