Skip to content

Commit a44531a

Browse files
committed
Fixed django#18862 -- Honored script prefix in FlatPage.get_absolute_url.
1 parent 4b01ee7 commit a44531a

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

django/contrib/flatpages/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.db import models
44
from django.contrib.sites.models import Site
5+
from django.core.urlresolvers import get_script_prefix
56
from django.utils.translation import ugettext_lazy as _
67
from django.utils.encoding import iri_to_uri, python_2_unicode_compatible
78

@@ -26,4 +27,5 @@ def __str__(self):
2627
return "%s -- %s" % (self.url, self.title)
2728

2829
def get_absolute_url(self):
29-
return iri_to_uri(self.url)
30+
# Handle script prefix manually because we bypass reverse()
31+
return iri_to_uri(get_script_prefix().rstrip('/') + self.url)

django/contrib/flatpages/tests/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import unicode_literals
44

5+
from django.core.urlresolvers import set_script_prefix, clear_script_prefix
56
from django.contrib.flatpages.models import FlatPage
67
from django.test import TestCase
78

@@ -12,4 +13,10 @@ def test_get_absolute_url_urlencodes(self):
1213
pf = FlatPage(title="Café!", url='/café/')
1314
self.assertEqual(pf.get_absolute_url(), '/caf%C3%A9/')
1415

15-
16+
def test_get_absolute_url_honors_script_prefix(self):
17+
pf = FlatPage(title="Tea!", url='/tea/')
18+
set_script_prefix('/beverages/')
19+
try:
20+
self.assertEqual(pf.get_absolute_url(), '/beverages/tea/')
21+
finally:
22+
clear_script_prefix()

django/core/urlresolvers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,15 @@ def get_script_prefix():
521521
"""
522522
return getattr(_prefixes, "value", '/')
523523

524+
def clear_script_prefix():
525+
"""
526+
Unsets the script prefix for the current thread.
527+
"""
528+
try:
529+
del _prefixes.value
530+
except AttributeError:
531+
pass
532+
524533
def set_urlconf(urlconf_name):
525534
"""
526535
Sets the URLconf for the current thread (overriding the default one in

0 commit comments

Comments
 (0)