URLResolver failing to find pattern match for admin POST request - cPanel/Apache

I have not solved this problem, but I have developed a workaround.

  1. Edit _get_response(self, request) in django.core.handlers.base. Change resolver_match = resolver.resolve(request.path_info) to resolver_match = resolver.resolve(request.path).

  2. Add this middleware (adjust to your exact needs):

     class ImageField404Middleware:
         def __init__(self, get_response):
             self.get_response = get_response
    
         def __call__(self, request):
             response = self.get_response(request)
    
             if (request.method == 'POST' and request.user.is_superuser and response.status_code == 302
                     and request.get_full_path().startswith('/pathtoadmin/')):
                 post_messages = get_messages(request)
                 for message in post_messages:
                     if ('was added successfully' in message.message or 'was changed successfully' in message.message
                             and message.level == message_levels.SUCCESS):
                         messages.success(request, message.message)
                         redirect_url = request.get_full_path()
                         if '_addanother' in request.POST:
                             redirect_url = re.sub(r'[^/]*/[^/]*/$', 'add/', redirect_url)
                         elif '_save' in request.POST:
                             redirect_url = re.sub(r'[^/]*/[^/]*/$', '', redirect_url)
                         return HttpResponseRedirect(redirect_url)
    
             return response
    

I have tested and this works with admin forms with ImageFields. Uploaded an image without issue.

Editing base.py isn’t ideal by any means but this is the least invasive solution I have found (indeed, the only thing that works). I don’t think using request.path instead of request.path_info will make a difference in my case, but other projects may not be able to do that.

It’s a horrible workaround, but it’s all I’ve got at the moment. Any thoughts in the absence of other solutions?

1 Like