Django in a subdirectory - admin site not working

I deployed the Django project in a subdirectory on the server (dns_name_of_my_page / notes). My application in this project is available through dns_name_of_my_page / notes / app /. Unfortunately, the admin site does not work if the entire Django project is located in the "notes" subdirectory in public_html on the hosting server. When I log in to the admin site (dns_name_of_my_page / notes / admin /), the redirect to the URL occurs without the name of the subdirectory (dns_name_of_my_page / adminlogin /? Next = // admin /), which is unacceptable. Should it be rather dns_name_of_my_page / notes / adminlogin /? Next = / notes / admin /

Here is my URL configuration in the project:

urlpatterns = [
    url(r'^/?app/?', include('app.urls')),
    url(r'^/?$', views.index, name='index'),
    url(r'^/?admin/?', admin.site.urls),
]

Here is my url configuration in my application:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<num>[0-9]+)/?', views.num, name='num'),
]

I tried to install

FORCE_SCRIPT_NAME = '/notes/'

or

SUB_SITE = "/notes/"

in settings.py, but that didn't help me.

- Django ?

+4
1

, [1], - .

- , . , , Django.

, , Django ( , Auth), - , django. , URI, http://my.server.com/project_name/foo/bar/ [2]. , my.server.com /foo/bar ( / run.py runerver)

NGinx -, 80 443 . location - Nginx + Gunicorn .

nginx :

location /project_name {
    proxy_set_header X-Forwarded-Protocol $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;

    proxy_pass http://localhost:7001/;
}

nginx + gunicorn , , , .

http://my.server.com/foo/bar/. Django, /project _name/prefix URL-, .

, , , :

USE_X_FORWARDED_HOST = True
FORCE_SCRIPT_NAME = '/project_name/'

Django , nginx , {% url name %} .

SESSION_COOKIE_PATH = '/project_name/'

, , ( ).

STATIC_FILES_ROOT='\path\on\disk\as\in\your\nginx\config\for\static\'
STATIC_FILES_URL='/project_name/static/'
MEDIA_FILES_ROOT='\path\on\disk\as\in\your\nginx\config\for\media\'
MEDIA_FILES_URL='/project_name/media/'

{% static blah%} .

LOGIN_REDIRECT_URL='/project_name/'
LOGOUT_REDIRECT_URL='/project_name/'

Auth / , . , url-conf, , admin

ADMIN_URL=r'^admin/'

urls.py:

url(settings.ADMIN_URL, include(admin.site.urls)),   # default=r'^admin/'

, / , , , , , FORCE_SCRIPT_NAME.

, , , /, - URL ( ), Django.

, , , django , - , , .


, :

urlpatterns = [
    url(r'^admin/?', admin.site.urls),
    url(r'^app/?', include('app.urls')),
    url(r'^$', views.index, name='index'),
]

, , , /home/root match come last. FORCE_SCRIPT_NAME .


[1], . django ; django ?; django suburl nginx

[2] SSL-, https, localhost ( ) http.

+1

Source: https://habr.com/ru/post/1681016/


All Articles