Django.core.urlresolvers.resolve improper deployment behavior without apache root

When a django application is deployed under a URL without apache root (with WsgiScriptAlias ​​/suburl/path_to_django.wsgi ), the {% url%} tag and django.core.urlresolvers.reverse takes into account the SCRIPT_NAME variable and returns the URLs of the form / suburl / path_to_my_view

However, when I use the django.core.urlresolvers.resolve function to resolve these URLs, it causes an error. This forces me to strip SCRIPT_NAME of the generated URLs before invoking the solution. Is this the expected behavior or am I misunderstanding everything?

Sincerely.

+4
source share
1 answer

I have the same problem:

  • SCRIPT_NAME defined in my apache configuration
  • calling django.core.urlresolvers.reverse outside wsgi did not add a prefix
  • in any view or resource, calling the same method adds a prefix

I managed to get the prefix automatically with the following lines of code:

 # in settings.py from django.core.urlresolvers import set_script_prefix ... FORCE_SCRIPT_NAME = "your-prefix" set_script_prefix(FORCE_SCRIPT_NAME) ... 

The first line ensures that your wsgi will use your prefix every time. The second sets the prefix, so reverse will find it.

Note that your apache conf must have the same prefix. This is a bit redundant, but I found a fix.

+1
source

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


All Articles