Exactly. The important thing to remember here is that, in Python 2, a "string" is actually a list of bytes, whereas a "unicode string" is actually a list of characters (not a list of unicode characters—just a list of characters). The confusion arises because, when Python was created, i.e. in 1990, multi-byte characters were in their infancy, and we mostly assumed that a character and a byte was more or less the same thing.

In Python 3, the Python 2 "strings" were renamed to "bytes", and the Python 2 "unicode strings" were renamed to "strings"—and the syntax for the literals of these types also changed. This made it way better.

More information about all this is in https://djangodeployment.com/2017/06/19/encodings-part-1/.



On 12/06/2022 12.07, Virgilio Ravagli wrote:
I found a way: book.title = titolo.decode('unicode_escape')
it works fine

Il giorno venerdì 10 giugno 2022 alle 21:45:46 UTC+2 Virgilio Ravagli ha 
scritto:

    Thank you, Antonis, it works !
    book.title = u'Verità' doesn't give any errors.

    Just another step: suppose that the title with accented letters stays in a
    variable, say titolo, and I want to write
    book.title = titolo. It gives error. How can I do, when the assignment on
    the right is not a constant ?
    Thanks in advance

    Il giorno venerdì 10 giugno 2022 alle 15:25:51 UTC+2 Antonis Christofides
    ha scritto:

        Hello,

        try this:

        book.title = u'Verità'

        Regards,

        Antonis

        P.S. Sorry I was a bit harsh yesterday—I had drunk too much beer :-)


        On 10/06/2022 10.50, Virgilio Ravagli wrote:
        I have surround book.save with a try...catch...; the exception is:
        You must not use 8-bit bytestrings unless you use a text_factory that
        can interpret 8-bit bytestrings (like text_factory = str). It is
        highly recommended that you instead just switch your application to
        Unicode strings.
        Without the try...catch, here is the traceback:
        Environment:


        Request Method: POST
        Request URL: http://localhost:8000/uti/dataLoading/

        Django Version: 1.8.5
        Python Version: 2.7.10
        Installed Applications:
        ('django.contrib.admin',
         'django.contrib.auth',
         'django.contrib.contenttypes',
         'django.contrib.sessions',
         'django.contrib.messages',
         'django.contrib.staticfiles',
         'uti')
        Installed Middleware:
        ('django.contrib.sessions.middleware.SessionMiddleware',
         'django.middleware.common.CommonMiddleware',
         'django.middleware.csrf.CsrfViewMiddleware',
         'django.contrib.auth.middleware.AuthenticationMiddleware',
         'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
         'django.contrib.messages.middleware.MessageMiddleware',
         'django.middleware.clickjacking.XFrameOptionsMiddleware',
         'django.middleware.security.SecurityMiddleware')


        Traceback:
        File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in
        get_response
          132.                     response = wrapped_callback(request,
        *callback_args, **callback_kwargs)
        File
        "C:\Users\RavagliV\eclipse-workspace-saved\librarian\uti\views.py" in
        datLoading
          34.                 msg = do_dat_loading()
        File
        "C:\Users\RavagliV\eclipse-workspace-saved\librarian\uti\views.py" in
        do_dat_loading
          79.             book.save()
        File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
          734.  force_update=force_update, update_fields=update_fields)
        File "C:\Python27\lib\site-packages\django\db\models\base.py" in
        save_base
          762.             updated = self._save_table(raw, cls, force_insert,
        force_update, using, update_fields)
        File "C:\Python27\lib\site-packages\django\db\models\base.py" in
        _save_table
          827. forced_update)
        File "C:\Python27\lib\site-packages\django\db\models\base.py" in
        _do_update
          877.         return filtered._update(values) > 0
        File "C:\Python27\lib\site-packages\django\db\models\query.py" in 
_update
          580.         return query.get_compiler(self.db).execute_sql(CURSOR)
        File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py"
        in execute_sql
          1062.         cursor = super(SQLUpdateCompiler,
        self).execute_sql(result_type)
        File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py"
        in execute_sql
          840.             cursor.execute(sql, params)
        File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in
        execute
          79.             return super(CursorDebugWrapper, self).execute(sql,
        params)
        File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in
        execute
          64.                 return self.cursor.execute(sql, params)
        File "C:\Python27\lib\site-packages\django\db\utils.py" in __exit__
          97.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
        File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in
        execute
          64.                 return self.cursor.execute(sql, params)
        File
        "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py" in
        execute
          318.         return Database.Cursor.execute(self, query, params)

        Exception Type: ProgrammingError at /uti/dataLoading/
        Exception Value: You must not use 8-bit bytestrings unless you use a
        text_factory that can interpret 8-bit bytestrings (like text_factory
        = str). It is highly recommended that you instead just switch your
        application to Unicode strings.

        Il giorno giovedì 9 giugno 2022 alle 23:17:16 UTC+2 Antonis
        Christofides ha scritto:

            Please read my email again.

            Antonis Christofides
            +30-6979924665  <tel:+30%20697%20992%204665>  (mobile)

            On 09/06/2022 22.27, Virgilio Ravagli wrote:
            All right.
            class Book(models.Model):
                   code = models.PositiveIntegerField(primary_key=True)
                   title =
            models.CharField(max_length=60,blank=False,null=False)

            book = Book()
            book.code = 1
            book.title = 'Verità'
            book.save()

            the code is really simple
            Il giorno mercoledì 8 giugno 2022 alle 21:07:41 UTC+2 Antonis
            Christofides ha scritto:

                Please provide the minimum code that reproduces the problem,
                and the full traceback.

                Regards,

                Antonis


                On 08/06/2022 18.24, Virgilio Ravagli wrote:
                ou must not use 8-bit bytestrings unless you use a
                text_factory that can interpret 8-bit bytestrings

-- 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 [email protected].
            To view this discussion on the web visit
            
https://groups.google.com/d/msgid/django-users/cf785c0e-5cfc-4587-b0cd-ea796a62a20bn%40googlegroups.com
            
<https://groups.google.com/d/msgid/django-users/cf785c0e-5cfc-4587-b0cd-ea796a62a20bn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 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 [email protected].
        To view this discussion on the web visit
        
https://groups.google.com/d/msgid/django-users/e2df5c07-5dd3-4fc3-9034-1955d438b670n%40googlegroups.com
        
<https://groups.google.com/d/msgid/django-users/e2df5c07-5dd3-4fc3-9034-1955d438b670n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fb8bed42-b1f9-4aba-aa9d-c87b06cc774cn%40googlegroups.com <https://groups.google.com/d/msgid/django-users/fb8bed42-b1f9-4aba-aa9d-c87b06cc774cn%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ec176e04-212b-13ea-e2b7-c112cefe184e%40antonischristofides.com.

Reply via email to