Incorrect Padding error

Hi! I’m relatively new to Django. Using Django 3.1.2 in Python 3.6.9 to build an app. Using a form from class to create an object, but when I click submit, I get the following and I’m not sure what ‘incorrect padding’ means. The object I’m attempt to create includes a CharField and TextField and the other form fields are not filled out (including an urlfields, emailfields, an imagefield and some datefields) …
Any help would be appreciated.:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/pages/person/add/

Django Version: 3.1.2
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrap4',
 'pinax.messages',
 'django_countries',
 'prisonerSolidarity.core',
 'prisonerSolidarity.pages',
 'prisonerSolidarity.users']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/views/generic/edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/views/generic/edit.py", line 141, in post
    if form.is_valid():
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/forms/forms.py", line 177, in is_valid
    return self.is_bound and not self.errors
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/forms/forms.py", line 172, in errors
    self.full_clean()
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/forms/forms.py", line 376, in full_clean
    self._post_clean()
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/forms/models.py", line 405, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/db/models/base.py", line 1209, in full_clean
    self.clean_fields(exclude=exclude)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/db/models/base.py", line 1251, in clean_fields
    setattr(self, f.attname, f.clean(raw_value, self))
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 650, in clean
    value = self.to_python(value)
  File "/home/anteo/.envs/prisonerSolidarity/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2295, in to_python
    return memoryview(b64decode(value.encode('ascii')))
  File "/usr/lib/python3.6/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)

Exception Type: Error at /pages/person/add/
Exception Value: Incorrect padding

It looks like at that point that you’re trying to convert from base64 to ascii - but the data isn’t properly formatted base64 data.

To provide more specific help, we’d need to see the form, view, and models involved.

Thank you for the quick response!

Model:

class Person(models.Model):

name = models.CharField(max_length=50)
slug = models.SlugField(allow_unicode=True, unique=True, null=False)
bio = models.TextField(blank=True)
created = models.DateField(auto_now_add=True)
modified = models.DateField(auto_now_add=True)
birthday = models.DateField(blank=True)
projected_release = models.DateField(blank=True)
next_parole_date = models.DateField(blank=True)
FACING_CHARGES = ‘FAC’
JAILED = ‘JAI’
IMPRISONED = ‘IMP’
RELEASED = ‘REL’
DECEASED = ‘DEC’
LEGAL_STATUS_CHOICES = [
(FACING_CHARGES, “Facing Charges, Not Incarcerated”),
(JAILED, “Jailed (awaiting charges or shorter sentence”),
(IMPRISONED, “Imprisoned”),
(RELEASED, “Released”),
(DECEASED, “Deceased”)
]
legal_status = models.CharField(
max_length=3,
choices=LEGAL_STATUS_CHOICES,
default=IMPRISONED)
mailing_address = models.CharField(max_length=150, blank=True)
imprisoned_by = CountryField()
mailing_suggestions = models.CharField(max_length=250, blank=True)
support_crew = models.BinaryField(default=False, editable=True)
support_email = models.EmailField(blank=True)
support_url = models.URLField(blank=True)
donation_url = models.URLField(blank=True)
wishlist_url = models.URLField(blank=True)
support_fb = models.URLField(blank=True)
support_twitter = models.URLField(blank=True)
support_mastadon = models.URLField(blank=True)
support_instagram = models.URLField(blank=True)
bitcoin = models.CharField(max_length=35, blank=True)
pic = models.ImageField(upload_to=‘page_pics/’, null=True, blank=True)
brochure = models.FileField(upload_to=‘brochures/’, null=True, blank=True)
adopter = models.ManyToManyField(User)

def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super().save(*args, **kwargs)

def str(self):
return self.name

View:

class PersonAdd(edit.CreateView, LoginRequiredMixin):
model = Person
form_class = PersonAddForm
template_name = ‘pages/person_add.html’

Form:

from django import forms
from django.utils.translation import gettext_lazy as _
from PIL import Image
from .models import Agency, Case, Movement, Person, Prison

class PersonAddForm(forms.ModelForm):
class Meta:
fields = [
‘name’,
‘bio’,
‘birthday’,
‘projected_release’,
‘next_parole_date’,
‘legal_status’,
‘mailing_address’,
‘imprisoned_by’,
‘mailing_suggestions’,
‘support_crew’,
‘support_email’,
‘support_url’,
‘donation_url’,
‘wishlist_url’,
‘support_fb’,
‘support_twitter’,
‘support_instagram’,
‘support_mastadon’,
‘bitcoin’,
‘pic’,
‘brochure’,
]
model = Person
labels = {
‘mailing_address’ : _(‘Last Known Address’),
}
widgets = {
‘birthday’ : forms.DateInput(format=’%B %d, %Y’),
‘projected_release’ : forms.DateInput(format=’%B %d, %Y’),
‘next_parole_date’ : forms.DateInput(format=’%B %d, %Y’),
}

Without having had a chance to actually try this, I’m going to go way out on a limb and say this might be the cause of the problem:

(Initial Conjecture with no direct knowledge to support this - “False” is a boolean type and may not be an appropriate default for that field. I’d be inclined to try it as null=True instead of default=False)

If I were investigating this, I’d probably first try to create a Person object from within the shell, without assigning a value to that field. If that works, then I’d want to figure out which field is causing the error.
Depending upon what I had set up in my development environment, I’d either:

  • Run it in the debugger to catch the exception to determine what field is causing the problem
    or
  • Run it under runserver_plus and use the Werkzeug browser-based debugging tool to see what’s happening.

Any steps beyond that are going to be determined by what is found above.

1 Like

Yeah, I don’t recall putting “BinaryField” in there, i meant BooleanField. I changed that, deleted migrations and database, ran the migrations and started the server and it seems to be working now with null=True and blank=True. I really appreciate you pointing that out and am going to keep plugging along. This was my first experience on this forum (mostly have been looking up answers in StackOverflow) and this was quite helpful. Thanks!