How to set base URL in Django

I would like to start Django from the location https://www.example.com/someuri/ , so I have the admin interface https://www.example.com/someuri/admin/ . I see a login window, but when I log in, I am redirected to https://www.example.com/admin/ .

Where can I set the base Django URL at https://www.example.com/someuri/ ? I tried with BASE_URL but no luck.

+4
source share
4 answers

This should work for you.

from django.conf.urls import patterns, url urlpatterns = patterns('', (r'^someuri/admin/', include(admin.site.urls) ), ) 
+2
source

I have a project with a similar setup. My django works using a virtual environment with apache. In my 000-default.conf file, I installed WSGIScriptAlias ​​in the following:

 WSGIScriptAlias /someuri /path/to/wsgi.py 

Here is the WSGIScriptAlias ​​documentation for serving the project from the subdirectory https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/#using-mod-wsgi-daemon-mode .

Than the following is specified in the urls.py file:

 from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^', include('someuri.urls')), url(r'^admin/', admin.site.urls), ] 

After logging in, I am redirected to http://example.com/someuri/admin/

+1
source
Answer to

@AgileDeveloper is completely correct for this admin case. However, is there any desire to set it this way for all URLs? If yes, maybe you should do something like this

 from django.conf.urls import include, url from . import views urlpatterns = [ url(r'^someuri/', include([ url(r'^admin/', include(admin.site.urls) ), url(r'^other/$', views.other) ])), ] 
0
source

@James Wallace's answer works well if you have control over the web server hosting Django at / someuri. In this configuration, the server will pass the value SCRIPT_NAME in the Django header, which then finds out that it is being served on this subclause.

However, if you do not have control over the external server, you can force Django to accept this value by adding settings.py to the project:

 USE_X_FORWARDED_HOST = True FORCE_SCRIPT_NAME = '/someuri' 

Subsequently, you may have problems with static files, css, etc. even in the admin application. In the default configuration, they are served by the Django development server unchanged. However, with the above changes, static files will be lost. Assuming that you still want to serve these files through the internal Django development server (and not through an external web server), you need to add settings.py to the 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 modify the 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', ], }, }, ] 

Then you will want to collect the static files in the specified directory:

 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) 

After that, the administration package should work fine, with the appropriate style sheets and everything. The only thing that doesn't seem to work well is the “SITE VIEW” link, as it skips the slash. I did not find a solution for this, but this is probably due to the hacking of the admin application.


In addition, there are various online manuals for installing Django at a subpath. It's messy, but the above avoids the worst headaches.

0
source

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


All Articles