Skip to main content

Add UTF-8 Validation to Django FileFields, CharFields and TextFields

Project description

Build status Coverage

Sometimes you want to only allow UTF-8 only content. This library extends the Django FileField, CharField and TextField by checking if the content of a file or text is UTF-8. If not, it generates an error. An extra check is executed for four byte long characters, which too can cause problems when trying to process a file as UTF-8.

Requirements

Django >= 1.8

Installation

pip install django-utf8field

Usage

Add the app to your settings:

INSTALLED_APPS = (
    ...
    'utf8field',
    ...

FileField

Create a model like you would do normally, but instead of using FileField you use UTF8FileField:

from django.db import models
from utf8field.fields import UTF8FileField

class YourModel(models.Model):
    title = models.CharField(max_length=255)
    created_on = models.DateTimeField(auto_add_on=True)
    text = models.UTF8FileField()

You also have the option to provide the option max_content_length to limit the number of characters in the file. If the content is longer an error will be displayed.

text = models.UTF8FileField(max_content_length=1000)

CharField

Create a model like you would do normally, but instead of using CharField you use UTF8CharField:

from django.db import models
from utf8field.fields import UTF8CharField

class YourModel(models.Model):
    title = models.CharField(max_length=255)
    created_on = models.DateTimeField(auto_add_on=True)
    text = models.UTF8CharField(max_length=1000)

TextField

Create a model like you would do normally, but instead of using TextField you use UTF8TextField:

from django.db import models
from utf8field.fields import UTF8TextField

class YourModel(models.Model):
    title = models.CharField(max_length=255)
    created_on = models.DateTimeField(auto_add_on=True)
    text = models.UTF8TextField()

Development

To run the tests make sure Django is installed (pip install django) and execute

python manage.py test

To create extra translations, execute

pm makemessages --locale=nl --extension=py --ignore=dev_example --ignore=build

… and modify the resulting django.po file in utf8field/locale/nl/LC_MESSAGES.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-utf8field-0.3.1.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

django_utf8field-0.3.1-py2.py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page