Categories
Python Answers

How to fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python?

Sometimes, we want to fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python.

In this article, we’ll look at how to fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python.

How to fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python?

To fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python, we can call open with the encoding argument.

For instance, we write

with open(csv_name_here, 'r', encoding="utf-8") as f:
    #...

to call open with the file path and the encoding argument set to 'utf-8' to open the CSV as a Unicode document.

This will make open decode the Unicode characters without errors.

Conclusion

To fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python, we can call open with the encoding argument.

Categories
Python Answers

How to format numbers in Python Django templates?

Sometimes, we want to format numbers in Python Django templates.

In this article, we’ll look at how to format numbers in Python Django templates.

How to format numbers in Python Django templates?

To format numbers in Python Django templates, we can use a filter.

For instance, we write

{% load humanize %}
{{ my_num|intcomma }}

to load the humanize filters with load humanize.

And then we apply the intcomma filter to format the my_num number to have comma digit group separators.

intcomma is one of the humanize filters.

Conclusion

To format numbers in Python Django templates, we can use a filter.

Categories
Python Answers

How to filter objects for count annotation in Python Django?

Sometimes, we want to filter objects for count annotation in Python Django.

In this article, we’ll look at how to filter objects for count annotation in Python Django.

How to filter objects for count annotation in Python Django?

To filter objects for count annotation in Python Django, we call annotate with the field argument set to the count result.

For instance, we write

from django.db.models import Q, Count
events = Event.objects.annotate(
    paid_participants=Count('participants', filter=Q(participants__is_paid=True))
)

to call annotate with the paid_participants argument set to the aggregation result of getting the count of the participants field values with Count.

We call Count with filter to do the filtering when aggregating the count.

Conclusion

To filter objects for count annotation in Python Django, we call annotate with the field argument set to the count result.

Categories
Python Answers

How to setup of Python logging in Django?

Sometimes, we want to setup of Python logging in Django.

In this article, we’ll look at how to setup of Python logging in Django.

How to setup of Python logging in Django?

To setup of Python logging in Django, we set the LOGGING variable in settings.py.

For instance, we write

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        }
    }
}

in settings.py to set the settings for logging.

handlers is a dict with the settings for logging various kinds of events.

And we add the handler names into the handlers setting in loggers.

class is the path to the logger class.

level is the logging level.

Conclusion

To setup of Python logging in Django, we set the LOGGING variable in settings.py.

Categories
Python Answers

How to set up a PostgreSQL database in Python Django?

Sometimes, we want to set up a PostgreSQL database in Python Django.

In this article, we’ll look at how to set up a PostgreSQL database in Python Django.

How to set up a PostgreSQL database in Python Django?

To set up a PostgreSQL database in Python Django, we add the psycopg2 package and add some settings into settings.py.

We run

pip install psycopg2

to install psycopg2.

Then in settings.py, we write

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',                      
        'USER': 'db_user',
        'PASSWORD': 'db_user_password',
        'HOST': '',
        'PORT': 'db_port_number',
    }
}

to set the DATABASE variable to a dict with the settings.

USER and PASSWORD ahs the user and password.

HOST and PORT has the URL and port of the database server.

Conclusion

To set up a PostgreSQL database in Python Django, we add the psycopg2 package and add some settings into settings.py.