Django管理员在POST上返回404,在GET上返回200 [英] Django admin returns 404 on POST, 200 on GET

查看:300
本文介绍了Django管理员在POST上返回404,在GET上返回200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在生产服务器上,当我尝试编辑/创建对象时,保存失败,返回404。这仅在POST请求中发生,GET请求(加载页面)可以正常工作。
Django是通过cPanel和WSGI部署的,DEBUG为False(尽管即使将其设置为True也不能使用)

On production server, when I try to edit/create an object then saving fails, returning 404. This only occurs for POST requests, GET requests (loading the page) work fine. Django is deployed via cPanel and WSGI, DEBUG is False (though it does not work even when it's set to True)

我已经使用cpanel,在virtualenv中运行。这似乎仅在使用 FileField 进行文件上传的模型上发生,其他模型可以更改/创建就很好。

I have deployed the Django app with cpanel, running in a virtualenv. This only seems to happen for a model that uses FileField for file upload, other models can be changed/created just fine.

模型:

def get_image_path(instance, filename):
    name, ext = filename.split('.')
    return f'images/{instance.advert_id}/{name}.{ext}'

class AdvertImage(models.Model):
    advert = models.ForeignKey(Advert, on_delete=models.CASCADE)
    image = models.ImageField(upload_to=get_image_path)

URL conf:

urlpatterns = [
                  path('i18n/', include('django.conf.urls.i18n')),
              ] + i18n_patterns(
    path('admin/', admin_site.urls),

    # ... other views....

    prefix_default_language=False
)

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

导航到 domain.com/admin/colli / advertimage / 1 / change / -页面正确加载。填写表单,单击保存。

Navigate to domain.com/admin/colli/advertimage/1/change/ - the page loads correctly. Fill the form, click save.

应保存该模型,并且不应出现404。

其他未使用<$ c的模型$ c> FileField ,因为它们所有管理员视图均正常工作

The model should be saved and no 404 should occur.
Other models, that do not use FileField, for them all admin views work correctly

如果DEBUG = True,则完整的错误消息为:

With DEBUG=True, the full error message is:

Page not found (404)
Request Method: POST
Request URL:    http://example.com/admin/colli/advertimage/1/change/
Raised by:  django.contrib.admin.options.change_view
Using the URLconf defined in Colli.urls, Django tried these URL patterns, in this order:

i18n/
admin/
[name='home']
advert/<slug:slug> [name='detail']
tagauks [name='admin_list']
tagauks/lisa [name='insert']
tagauks/muuda/<int:pk> [name='edit']
tagauks/kasutaja/<int:pk> [name='user_edit']
tagauks/save_image [name='save_image']
accounts/login/ [name='login']
accounts/logout/ [name='logout']
api/
^media/(?P<path>.*)$
The current path, colli/advertimage/1/change/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

引起这种情况的视图似乎是 change_view

The view causing this seems to be change_view

推荐答案

您是否已激活中间件(国际化:采用网址格式)?

Have you activated middleware (Internationalization: in URL patterns)?

# settings.py
MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware'
...
]

这篇关于Django管理员在POST上返回404,在GET上返回200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆