How to set base URL in Django

后端 未结 4 492
故里飘歌
故里飘歌 2021-01-04 09:30

I would like to run Django from location https://www.example.com/someuri/ so that I have the admin interface at https://www.example.com/someuri/admin/. I can see the login w

4条回答
  •  孤街浪徒
    2021-01-04 10:05

    The answer of @James Wallace works well assuming you have control of the web server hosting Django at /someuri. In that configuration, the server will pass a value of SCRIPT_NAME in the header to Django, who will then know it is being served on that sub-path.

    However, if you do not have control of the front-end server, then you can force Django to assume that value by adding to settings.py in the project:

    USE_X_FORWARDED_HOST = True
    FORCE_SCRIPT_NAME = '/someuri'
    

    Afterwards, you may still have problems with static files, the css, etc., even in the admin application. In the default config, these are served by the Django development server without changes. However, with the above changes the static files will be lost. Assuming you still want to serve these files through the internal Django development server (and not through an external web server) you need to add to the settings.py project file:

    STATIC_SUFFIX = '/static/'
    STATIC_URL = FORCE_SCRIPT_NAME + STATIC_SUFFIX
    MEDIA_SUFFIX = '/media/'
    MEDIA_URL = FORCE_SCRIPT_NAME + MEDIA_SUFFIX
    

    In the same file, you also need to change TEMPLATES and add the following:

    STATIC_ROOT = os.path.join(BASE_DIR, "static/")
    
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [ STATIC_ROOT ],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    

    You will then want to gather the static files into the directory defined:

    python manage.py collectstatic
    

    or

    python3 manage.py collectstatic
    

    The project level urls.py file should look like this (Django v1.11):

    import os
    from django.conf.urls import url
    from django.contrib import admin
    from django.conf.urls.static import static
    from django.conf import settings
    from django.views.generic.base import TemplateView
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
    ] + static(settings.STATIC_SUFFIX, document_root=settings.STATIC_ROOT)
    

    Thereafter, the admin package should work okay, with appropriate stylesheets and everything. The only thing that does not seem to work well is the "VIEW SITE" link since it misses a slash. I did not find a solution for that but probably it involves hacking the admin application.


    Beyond that, there are various guides online for installing Django under a sub-path. It's messy but the above avoids the worst headaches.

提交回复
热议问题