开发者

django.core.urlresolvers.resolve incorrect behavior under apache non-root deployment

开发者 https://www.devze.com 2023-02-10 04:34 出处:网络
When a django app is deployed under a non-root apache url (with WsgiScriptAlias /suburl /path_to_django.wsgi) the {%url%} tag and the django.core.urlresolvers.reverse function take into account the SC

When a django app is deployed under a non-root apache url (with WsgiScriptAlias /suburl /path_to_django.wsgi) the {%url%} tag and the django.core.urlresolvers.reverse function take into account the SCRIPT_NAME variable and return urls of the form /suburl/path_to_my_view

However, when I use the django.core.urlresolvers.resolve function to resolve those urls it throws an error. That forces me to strip the SCRIPT_NAME of the generated ur开发者_如何学运维ls before calling resolve. Is this the expected behavior or am I misunderstanding everything?

Regards.


I got the same kind of problem:

  • SCRIPT_NAME defined in my apache config
  • a call to django.core.urlresolvers.reverse outside the wsgi didnt prepend the prefix
  • in any view or resource, a call to the same method prepended the prefix

I managed to have the prefix automatically prepended using the next 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 makes sure your wsgi uses your prefix every time. The second one sets the prefix, so that reverse will find it.

Please note that you need to have the same prefix in your apache conf. It's a bit redundant, but the cleaner fix I found.

0

精彩评论

暂无评论...
验证码 换一张
取 消