Django prefix for all generated URLs

My proxy (nginx 80 public port) in django (wsgi localhost port 8000 Puerto Ricon) removes the path to the "/ app" application, so requests for http://server/app/hello appear as django as /hello and requests for http://server/app/admin appears as django as /admin .

The problem is that the admin site generates output with absolute URLs such as <form action="/admin/"... on the login screen. Thus, the user sees the administrator login screen, but he is sent to http://server/admin , which does not exist. (I kind of hoped that it would use relative URLs and work anywhere.)

What is the easiest way to make admin pages universally add "/ app", for example, form action="/app/admin/" to all the URLs it generates in the page output? I hope for something built into Django as a simple definition, and there is no need to create special filters and then rewrite the templates to use them, but I can not find it.

+7
source share
2 answers

See the documentation for FORCE_SCRIPT_NAME:

https://docs.djangoproject.com/en/dev/ref/settings/#force-script-name

Set the value to / app instead of the default value of None.

+11
source

The easiest way is to write your own wsgi manager, for example below

 def application(environ, request_response): # do whatever you want with path sys.path.append(path_to_django_project) os.environ['DJANGO_SETTINGS_MODULE'] = 'proj_name.settings' # pass control to django import django.core.handlers.wsgi app_entry_point = django.core.handlers.wsgi.WSGIHandler() return app_entry_point(environ,request_response) 
0
source

All Articles