Tl; dr: Is there a way to override the default behaviour of reverse
?
In my django project I have alot of urls such as
url(r'^w+/company/', include("company.urls", namespace="company")),
Which allows for urls such as
.../companyA/company/
.../companyB/company/
So that I can then use a custom middleware to modify the request to include some specific details based upon what company is using my site
This all works fine except for when django is trying to decipher the full path with reverse
and {% url .. %}
...
It seems to be returning /x/company/
as a default match for the regex. since the django.utils.regex_helper
method next_char
has an escape mapping for w
to map to x
The url
tag I have been able to override to replace the /x/
with the correct company name and I am wondering if there is a similar thing I can do to override reverse
in the same way, or anything else that I can do to resolve this problem?
Previously, I was using
url(r'^(?P<company_name>w+)/company/', include("company.urls", namespace="company"))
But this meant I had to include a parameter in every view
def view(request, company_name):
...
As well as include it in all my other calls to the view (i.e with the {% url %}
) which I am trying to avoid.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…