django.contrib.admin.options.add_view 404 with image field

I’ve moved my app to my server for production.
Some of my models work as expected, i can upload them via admin, see them etc.

The ones that return django.contrib.admin.options.add_view 404 error, contain image fields.
I’ve tested the MEDIA_URL and it returns an image as expected. The MEDIA_ROOT is set to what i need it to be.

I’ve looked around the internet for 2 days now, all solutions i have found are regarding development. On development, everything is fine.

Can someone help to point me in the right direction?

We would need to see all the relevant code - the views, models, templates and settings involved in this. We’d need to know more about your production environment - what web server are you running? What’s your configuration for that server regarding medial files? It would also be helpful to see the rendered html as seen through the browser’s development tools.

The more information you provide, the better the chances are that someone here can see what might be going wrong and offer suggestions.

Also, when posting code here, enclose the segments between lines consisting of only three backtick (`) characters - this preserves the formatting of the code.

For example:

# The previous line is ```
def some_function(self):
    for element in list:
# The next line is ```

I’m using the default admin area.
I’m on an Apache server (shared hosting)

Media settings.
MEDIA_ROOT = '/home/removed/'

The 404 page:

Page not found (404)

Request Method: POST
Request URL:[app]/[model]/add/
Raised by: django.contrib.admin.options.add_view

Using the URLconf defined in .urls , Django tried these URL patterns, in this order:

  1. admin/
    //etc etc

The current path, [appname]/[modelname]/add/ , didn’t match any of these.

As mentioned, this works in development, just not in production. I have found reference to this issue elsewhere but never a solution, the classic case of ‘Solved it, i fixed the issue’ and they never give details.

In the absence of any detail information being provided, I can only make some guesses.

I do see where you have two consecutive periods in the Request URL - but since you’re getting this error from Django, that’s probably not the root cause of the issue.

Since your MEDIA_URL appears to be pointing to a different host than your site itself, it’s probably not an issue with your Apache .conf file.

It looks like this is being caused from a page in the admin app, so we don’t need to see the view or the templates - but would need to see the ModelAdmin object.

There are many differences between configuring a Django project for a production environment and running it in the development environment, and getting any one of them wrong will cause problems.

However, without seeing more, the best I can do is point you toward the appropriate documentation:

That’s excellent, thank you Ken. I will look into the AdminModel aspect shortly. this isnt something any of the other topics mentioned.

I will hopefully return with a solution.

Hi Ken,

The repeated periods are from adding REMOVED but with brackets.

I haven’t specified a ModelAdmin.
The models causing issue contain:
coat_of_arms = models.ImageField(upload_to='coat_of_arms', null=True, blank=True)
I cannot see any other settings or changes that may influence this?

Sorry if i’ve been light on specifics, most of the settings and things are default. I will try and post anything you need specifically.

In my searches today i came across a new post on StackOverflow about this issue.

Previous posts

stackoverflow. com/questions/57340045/404-error-after-submitting-form-with-imagefield-on-django-admin

As you can see they all point to the same issue but no solution.

The list of all the useful information are in my replies above.

Yes, it’s a lot.

Yes, there are a lot of moving pieces in something like this.

And yes, it’s generally acknowledged that moving a Django-site to production the first time is one of the most painful parts of the entire process - but that’s because there are so many options and so many places where things can go wrong.

We could go through this one file at a time, but that would greatly drag out the process as I may need to go through a dozen files or so to eventually identify the root cause of the issue.



class AbstractLodge(models.Model):
    number = models.IntegerField(null=True, blank=True)
    name = models.CharField(max_length=100)
    coat_of_arms = models.ImageField(upload_to='coat_of_arms/', null=True, blank=True)
    consecrated = models.DateField()
    installation_month = models.CharField(
            ('JAN', 'January'),
            ('FEB', 'February'),
            ('MAR', 'March'),
            ('APR', 'April'),
            ('MAY', 'May'),
            ('JUN', 'June'),
            ('JUL', 'July'),
            ('AUG', 'August'),
            ('SEP', 'September'),
            ('OCT', 'October'),
            ('NOV', 'November'),
            ('DEC', 'December'),
    meeting = models.OneToOneField(Meeting, on_delete=models.SET_NULL, null=True, blank=True)
    email = models.CharField(max_length=200, blank=True)
    website = models.CharField(max_length=255, blank=True)

    def __str__(self):

    class Meta:
        abstract = True

class GrandLodge(AbstractLodge):
    class Meta(AbstractLodge.Meta):
        abstract = False

class ProvincialGrandLodge(AbstractLodge):
    grand_lodge = models.ForeignKey(GrandLodge, on_delete=models.CASCADE)
    province = models.OneToOneField(Province, on_delete=models.CASCADE)

class PrivateLodge(AbstractLodge):
    provincial_grand_lodge = models.ForeignKey(ProvincialGrandLodge, on_delete=models.CASCADE)
    area = models.ForeignKey(Area, on_delete=models.SET_NULL, null=True)


Debug = False

SECRET_KEY = os.environ['SECRET_KEY']


    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dontbother',
        'USER': 'dontbother',
        'PASSWORD': 'dontstealit',
        'HOST': '',
        'PORT': '3306',

STATIC_ROOT = '/home/ryanuwmx/'
STATIC_URL = '/static/'
MEDIA_ROOT = '/home/ryanuwmx/'

The issue is in the admin, so i dont have views or templates to show.

The other item that jumps out to me is that you’re getting a 404 in the admin area - taking that at face value indicates the possibility that you don’t have your admin class set up correctly for the model being referenced.

There are two more pieces to this puzzle - what was going on when this error is received? Were you going to a url, if so, which one; or, if you were clicking on a link on a page and that link generated this error, what was the link?
Also, what was the rendered HTML on that part of the page containing the link? (It would be best to see the rendered HTML as shown by the browser’s developer’s tools and not the “view source” from the visible page.)

I was saving a model entry in the admin area, on submission the 404 error was shown. As mentioned previously, development it works and non-image models work correctly.

I havent set up any special rules for my admin class, just added the models to allow them to be added via admin.

I’m sorry, I should have been more clear - I need to see the page where you click on “something” that ends up generating this error.

So it would be the page you’re seeing before you see this page.

There’s a URL being referenced that’s not valid - I’m looking to see what that URL is, and the context in which it’s being referenced.

It’s a default admin page:

It happens on any save option

I’m not seeing anything specifically wrong. I will admit I’ve never set up a MEDIA_URL that’s not on the same host as the server - that’s the only thing that looks “odd” to me, but I can’t find any specific reference to that being a problem. I’d be inclined to try it with the MEDIA_URL that’s relative to my current host, not on a separate system, but that’s only because I’ve never done that before and would be wanting to trace through the entire upload/submission process to see exactly where that error’s being thrown.

But beyond that, you’ve mentioned that it was working in your test environment - what are the complete set of changes that you’ve made in moving from your development environment to your production environment?

All i changed was the settings for production as seen before.

Is there a way to get a more detailed failure log?
I just dont see how only those admin add pages are 404 but the rest arent. It has to be something else causing it but the error reporting seems limited.

Thanks for your time Ken, I’m giving up.

Hello, I have been experiencing this problem for a while now, and it recently seems to have gotten worse.

I’ve posted my question on StackOverflow:

I am getting a 404 error on any admin change form with an ImageField when I submit the POST. I am also on an Apache server.

It only happens in production; when I run the app locally, it works 100% as expected. The GET request resolves fine in production so the path obviously exists.

Previously, this only occurred at random, but now it happens to every model with an ImageField.

I have dug through code, changed just about everything I can think of and I have gotten absolutely nowhere.

I’m leaning towards this being a server issue but I have no idea where to go from here.

Any advice would be greatly appreciated. I’m on the verge of stripping out all ImageFields…

I’ve developed a temporary workaround that may work for you until a cleaner solution becomes available:

Hello there, I am experiencing the same problem and I want to write my case for anyone who tries to solve ths issue.
I did a local setup for Google Cloud to do the work of a CDN and it worked fine locally. But when I uploaded the changes in my webserver when I tried to save an object with an ImageField I got an error 404, Raised by: django.contrib.admin.options.add_view.

I don’t have any clue how to solve this.

hello, can you tell me how you solved this problem

This issue has no relationship with python or django.
this issue because of the ModSecurity of hosting. you can disable the ModSecurity via Cpanel or via contact with hosting support and tell them to disable the ModSecurity because prevents post the images and files. but ModSecurity is very important because it prevents the hacker attack.