14👍
@AgileDeveloper’s answer is completely correct for that one off case of admin. However, is the desire to set it this way for all URLs? If so, perhaps 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)
])),
]
8👍
I have a project that has a similar setup. I have django running using a virtual environment with apache. In my 000-default.conf file, I set the WSGIScriptAlias to the following:
WSGIScriptAlias /someuri /path/to/wsgi.py
Here is the documentation on WSGIScriptAlias to serve the project from a subdirectory, https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/#using-mod-wsgi-daemon-mode.
Than in the urls.py file I set the following:
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/
- [Django]-How to check if ManyToMany field is not empty?
- [Django]-How to define default data for Django Models?
- [Django]-Create django super user in a docker container without inputting password
5👍
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.
- [Django]-How to upgrade django?
- [Django]-Django [Errno 13] Permission denied: '/var/www/media/animals/user_uploads'
- [Django]-(Django) Trim whitespaces from charField
1👍
This should work for you.
from django.conf.urls import patterns, url
urlpatterns = patterns('',
(r'^someuri/admin/', include(admin.site.urls) ),
)
- [Django]-How to make Django's DateTimeField optional?
- [Django]-Getting 'DatabaseOperations' object has no attribute 'geo_db_type' error when doing a syncdb
- [Django]-Organizing Django unit tests