在POST时,Django Admin - FORCE_SCRIPT_NAME会在URL中附加两次

时间:2022-06-30 04:34:23

I have deployed Django to a sub directory (I don't have full control over the server so can't change the way it's deployed).

我已经将Django部署到子目录(我没有对服务器的完全控制,因此无法改变它的部署方式)。

I added to my settings:

我添加到我的设置:

FORCE_SCRIPT_NAME = '/hub06'
STATIC_URL = FORCE_SCRIPT_NAME + '/static/'

Now when I go to /admin/hub06, it's working properly, I can login and browse all admin pages. As soon as I do a POST request though (adding a new model), the URL gets corrupted.

现在,当我转到/ admin / hub06,它正常工作,我可以登录并浏览所有管理页面。只要我执行POST请求(添加新模型),URL就会被破坏。

For example, when editing /hub06/admin/myapp/car/1

例如,编辑/ hub06 / admin / myapp / car / 1时

When I submit the form, it redirects to /hub06/hub06/admin/myapp/car/

当我提交表单时,它会重定向到/ hub06 / hub06 / admin / myapp / car /

So it adds script name twice. This is only for POST requests in Django admin.

所以它添加了两次脚本名称。这仅适用于Django admin中的POST请求。

2 个解决方案

#1


6  

Is this a linux host? is it running apache, nginx? It all depends on how your web server is configured.

这是一个linux主机吗?是运行apache,nginx?这完全取决于您的Web服务器的配置方式。

If you really must have a url prefix like /hub06/ then you will need to update any settings in settings.py that return a url such as LOGIN_URL, STATIC_URL, LOGIN_REDIRECT_URL etc to contain the prefix.

如果你真的必须有像/ hub06 /这样的url前缀,那么你需要更新settings.py中的任何设置,这些设置返回一个URL,例如LOGIN_URL,STATIC_URL,LOGIN_REDIRECT_URL等,以包含前缀。

I don't think you need to use FORCE_SCRIPT_NAME. Comment that bit out in the settings.py and update urls.py to add the following for example:

我认为你不需要使用FORCE_SCRIPT_NAME。注释在settings.py中输入并更新urls.py以添加以下示例:

from django.conf.urls import patterns, include, url
from django.contrib.auth.views import login
from django.contrib import admin
admin.autodiscover()

urlpatterns2 = patterns('',
    url(r'^$', 'yourapp.views.home', name='Home'),
    url(r'^admin/', include(admin.site.urls)),
)

urlpatterns = patterns('',
    url(r'^hub06/', include(urlpatterns2)),
)

Let me know how you go.

让我知道你怎么去。

#2


-1  

This was the solution:

这是解决方案:

# NB - this setting is required to make the app work correctly when running
# via ProxyPass from Apache. Otherwise CSRF checks and some redirects will not
# work.
USE_X_FORWARDED_HOST = True

#1


6  

Is this a linux host? is it running apache, nginx? It all depends on how your web server is configured.

这是一个linux主机吗?是运行apache,nginx?这完全取决于您的Web服务器的配置方式。

If you really must have a url prefix like /hub06/ then you will need to update any settings in settings.py that return a url such as LOGIN_URL, STATIC_URL, LOGIN_REDIRECT_URL etc to contain the prefix.

如果你真的必须有像/ hub06 /这样的url前缀,那么你需要更新settings.py中的任何设置,这些设置返回一个URL,例如LOGIN_URL,STATIC_URL,LOGIN_REDIRECT_URL等,以包含前缀。

I don't think you need to use FORCE_SCRIPT_NAME. Comment that bit out in the settings.py and update urls.py to add the following for example:

我认为你不需要使用FORCE_SCRIPT_NAME。注释在settings.py中输入并更新urls.py以添加以下示例:

from django.conf.urls import patterns, include, url
from django.contrib.auth.views import login
from django.contrib import admin
admin.autodiscover()

urlpatterns2 = patterns('',
    url(r'^$', 'yourapp.views.home', name='Home'),
    url(r'^admin/', include(admin.site.urls)),
)

urlpatterns = patterns('',
    url(r'^hub06/', include(urlpatterns2)),
)

Let me know how you go.

让我知道你怎么去。

#2


-1  

This was the solution:

这是解决方案:

# NB - this setting is required to make the app work correctly when running
# via ProxyPass from Apache. Otherwise CSRF checks and some redirects will not
# work.
USE_X_FORWARDED_HOST = True