The Python Oracle

Django Admin Intermittently Returning 404 On Model Edit

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities

--

Chapters
00:00 Django Admin Intermittently Returning 404 On Model Edit
01:04 Answer 1 Score 0
01:17 Accepted Answer Score 4
01:41 Answer 3 Score 1
02:30 Thank you

--

Full question
https://stackoverflow.com/questions/3158...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #djangoadmin #httpstatuscode404

#avk47



ACCEPTED ANSWER

Score 4


I had this same problem. The solution for me was to move my calls to admin.site.register() to admin.py. With DEBUG set to false, your models are lazily loaded, so the registration calls don't always get made. Apparently admin.py is always loaded at init time, however.




ANSWER 2

Score 1


i had same problem until last week. after i track this error for couple of months and i found 404 raising at django source code.

i modified the file /path/to/django/contrib/admin/options.py get_object() method of ModuleAdmin class. Note: i use Django 1.3.1

somehow django cannot find object with pk object_id in queryset. so i modified it like this:

def get_object(self, request, object_id):
    ....
    queryset = self.queryset(request)
    model = queryset.model
    obj = None

    #first search the object with original way
    try:
        object_id = model._meta.pk.to_python(object_id)
        obj = queryset.get(pk=object_id)
    except:
        #print "DEBUG: > first try does not exist (%s)" % str(object_id)
        obj = None

    if obj is None: 
        #if object doesn't exist in queryset, search in db
        try:
            object_id = model._meta.pk.to_python(object_id)
            obj = model.objects.get(pk=object_id)
            #print "DEBUG: > second try found %s" % str(obj)
        except (model.DoesNotExist, ValidationError):
            #print "DEBUG: > second try does not exist"
            obj = None
    return obj

i know it's not a good thing to change something in django's source, so use at your own risk !




ANSWER 3

Score 0


See if the alternate WSGI script at the end of:

http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

makes a difference.