Fixed #22261 -- Fixed resolving namespaced URLs for flatpages.

This commit is contained in:
Hasan Ramezani 2019-11-27 00:00:56 +01:00 committed by Mariusz Felisiak
parent e2fab0b679
commit 2633c5341e
5 changed files with 38 additions and 4 deletions

View File

@ -1,6 +1,6 @@
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.db import models from django.db import models
from django.urls import get_script_prefix from django.urls import NoReverseMatch, get_script_prefix, reverse
from django.utils.encoding import iri_to_uri from django.utils.encoding import iri_to_uri
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -36,5 +36,12 @@ class FlatPage(models.Model):
return "%s -- %s" % (self.url, self.title) return "%s -- %s" % (self.url, self.title)
def get_absolute_url(self): def get_absolute_url(self):
from .views import flatpage
for url in (self.url.lstrip('/'), self.url):
try:
return reverse(flatpage, kwargs={'url': url})
except NoReverseMatch:
pass
# Handle script prefix manually because we bypass reverse() # Handle script prefix manually because we bypass reverse()
return iri_to_uri(get_script_prefix().rstrip('/') + self.url) return iri_to_uri(get_script_prefix().rstrip('/') + self.url)

View File

@ -0,0 +1,6 @@
from django.contrib.flatpages import views
from django.urls import path
urlpatterns = [
path('flatpage/', views.flatpage, {'url': '/hardcoded/'}),
]

View File

@ -0,0 +1,5 @@
from django.urls import include, path
urlpatterns = [
path('flatpage', include('django.contrib.flatpages.urls')),
]

View File

@ -1,5 +1,5 @@
from django.contrib.flatpages.models import FlatPage from django.contrib.flatpages.models import FlatPage
from django.test import SimpleTestCase from django.test import SimpleTestCase, override_settings
from django.test.utils import override_script_prefix from django.test.utils import override_script_prefix
@ -17,3 +17,16 @@ class FlatpageModelTests(SimpleTestCase):
def test_str(self): def test_str(self):
self.assertEqual(str(self.page), '/café/ -- Café!') self.assertEqual(str(self.page), '/café/ -- Café!')
@override_settings(ROOT_URLCONF='flatpages_tests.urls')
def test_get_absolute_url_include(self):
self.assertEqual(self.page.get_absolute_url(), '/flatpage_root/caf%C3%A9/')
@override_settings(ROOT_URLCONF='flatpages_tests.no_slash_urls')
def test_get_absolute_url_include_no_slash(self):
self.assertEqual(self.page.get_absolute_url(), '/flatpagecaf%C3%A9/')
@override_settings(ROOT_URLCONF='flatpages_tests.absolute_urls')
def test_get_absolute_url_with_hardcoded_url(self):
fp = FlatPage(title='Test', url='/hardcoded/')
self.assertEqual(fp.get_absolute_url(), '/flatpage/')

View File

@ -31,5 +31,8 @@ class FlatpagesSitemapTests(TestCase):
def test_flatpage_sitemap(self): def test_flatpage_sitemap(self):
response = self.client.get('/flatpages/sitemap.xml') response = self.client.get('/flatpages/sitemap.xml')
self.assertIn(b'<url><loc>http://example.com/foo/</loc></url>', response.getvalue()) self.assertIn(b'<url><loc>http://example.com/flatpage_root/foo/</loc></url>', response.getvalue())
self.assertNotIn(b'<url><loc>http://example.com/private-foo/</loc></url>', response.getvalue()) self.assertNotIn(
b'<url><loc>http://example.com/flatpage_root/private-foo/</loc></url>',
response.getvalue(),
)