5

I’m currently debugging a weird problem with a Django site where one specific model is triggering a 404 error when creating a new instance or editing an existing one in the admin interface.

Specifically, the error occurs when the form is submitted. I can GET the changeform just fine.

This is only occuring on the live site and only when saving this model. All other models behave as expected, and when I run it locally, everything works as expected. When created programatically, everything is also fine both live and locally.

Here’s my model:

class Content(models.Model):
    """Base Content class."""
    title = models.CharField(max_length=200)
    body = RichTextUploadingField(max_length=30000, blank=True, null=True, config_name='admin')
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(to=User, on_delete=models.CASCADE)
    slug = models.SlugField(max_length=100, null=True, default=None)

    class Meta:
        abstract = True


class ContentPage(Content):
    """Represents a page of generic text content."""
    title = models.CharField(max_length=200, unique=True)
    has_url = models.BooleanField(default=False, help_text='Sets the page to accessible via a URL.')
    banner = models.ImageField(upload_to='myfiles/banners/', blank=True, null=True)

    def save(self, *args, **kwargs):
        """Create the slug from the title."""
        self.slug = slugify(self.title[:100])
        super(ContentPage, self).save(*args, **kwargs)

The ContentPage class is the one triggering the problem in the admin interface. My other class that inherits from Content works fine.

I have stripped back my admin setup to the following and it is still occuring:

class CustomAdminSite(AdminSite):

    def get_urls(self):
        """Define custom admin URLs."""
        urls = super(CustomAdminSite, self).get_urls()

        # Append some new views here...

        return urls


admin_site = CustomAdminSite()
admin_site.register(ContentPage)

Here’s a basic URL config that reproduces the problem:

from myapp.admin import admin_site


urlpatterns = [
    path('mycooladminsite/', admin_site.urls),
    path('', include('myapp.urls')),
]

A few other things I’ve checked:

  • I have no signals interferring with the save.
  • I am not performing any actions in the model’s save() method.
  • I have checked for get_object_or_404() calls and can’t see any that would affect this.

I’ve spent a few hours digging through this and I’m currently at a brick wall.

The database engine is mysql.connector.django, with Django version 2.2.11. I can’t change the engine or update Django to 3.x yet for this site.

This is a recent problem that I did not notice previously.

Update:

I’ve narrowed down the problem to the ImageField. When I remove that from the fields displayed, the problem does not occur on save.

I’m using a custom admin form but that doesn’t seem to be the problem. I’ve tried using the default and it still occurs. I’ve been looking for exceptions in the storage class but haven’t found any. I stripped it all the way back and the error remains.

I’ve examined the local 302 POST and production 404 POST in Firefox Developer Tools and they’re virtually identical but production server is Apache and x-powered-by is Phusion Passenger, whereas locally the server is WSGIServer/0.2 CPython/3.7.3.

I’ve actually noticed a 404 occurring with other multipart/form-data forms in production now, which I never got before.