Categories
Python Answers

How to set up custom middleware in Python Django?

Sometimes, we want to set up custom middleware in Python Django.

In this article, we’ll look at how to set up custom middleware in Python Django.

How to set up custom middleware in Python Django?

To set up custom middleware in Python Django, we create our own class.

For instance, we write

class CustomMiddleware(object):
    def __init__(self, get_response):
        #...
        self.get_response = get_response

    def __call__(self, request):
        #...
        response = self.get_response(request)
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        #...
        return None

    def process_exception(self, request, exception):
        #...
        return None

    def process_template_response(self, request, response):
        #...
        return response

to create the CustomMiddleware class.

We add the __init__ method to initialize the middleware.

__call__ is called on each request.

process_view is called before Django calls the view.

process_exception is called when a view raises an exception.

And process_template_response is called after the view finished executing.

Then we activate our middleware by putting it in the MIDDLEWARE list in the settings.py file.

We write

MIDDLEWARE = [
    # ...
    # Add your custom middleware
    'path.to.your.middleware.CustomMiddleware',
]

to add our CustomMiddleware class into the list.

Conclusion

To set up custom middleware in Python Django, we create our own class.

Categories
Python Answers

How to access session variables from within a template with Python Django?

Sometimes, we want to access session variables from within a template with Python Django.

In this article, we’ll look at how to access session variables from within a template with Python Django.

How to access session variables from within a template with Python Django?

To access session variables from within a template with Python Django, we can use the request.session property.

For instance, we write

{{ request.session.name }}

to get the name value from request.session in our template.

Conclusion

To access session variables from within a template with Python Django, we can use the request.session property.

Categories
Python Answers

How to fix ‘You are trying to add a non-nullable field ‘new_field’ to userprofile without a default’ error with Python Django?

Sometimes, we want to fix ‘You are trying to add a non-nullable field ‘new_field’ to userprofile without a default’ error with Python Django.

In this article, we’ll look at how to fix ‘You are trying to add a non-nullable field ‘new_field’ to userprofile without a default’ error with Python Django.

How to fix ‘You are trying to add a non-nullable field ‘new_field’ to userprofile without a default’ error with Python Django?

To fix ‘You are trying to add a non-nullable field ‘new_field’ to userprofile without a default’ error with Python Django, we can set a default value for the non-nullable field.

For instance, we write

new_field = models.CharField(max_length=140, default='DEFAULT VALUE')

to set the default value of new_field to 'DEFAULT VALUE' by calling the CharField constructor with the default argument.

Conclusion

To fix ‘You are trying to add a non-nullable field ‘new_field’ to userprofile without a default’ error with Python Django, we can set a default value for the non-nullable field.

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.