Fixed #31527 -- Allowed admindocs index to handle non-string URLconfs.

This commit is contained in:
Jim Xie 2021-02-03 15:31:20 +01:00 committed by Carlton Gibson
parent e17bdb953a
commit ce60d28929
3 changed files with 25 additions and 5 deletions

View File

@ -4,7 +4,6 @@ from inspect import cleandoc
from pathlib import Path from pathlib import Path
from django.apps import apps from django.apps import apps
from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.contrib.admin.views.decorators import staff_member_required from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.admindocs import utils from django.contrib.admindocs import utils
@ -120,8 +119,11 @@ class ViewIndexView(BaseAdminDocsView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
views = [] views = []
urlconf = import_module(settings.ROOT_URLCONF) url_resolver = get_resolver(get_urlconf())
view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns) try:
view_functions = extract_views_from_urlpatterns(url_resolver.url_patterns)
except ImproperlyConfigured:
view_functions = []
for (func, regex, namespace, name) in view_functions: for (func, regex, namespace, name) in view_functions:
views.append({ views.append({
'full_name': get_view_name(func), 'full_name': get_view_name(func),

View File

@ -38,7 +38,8 @@ Minor features
:mod:`django.contrib.admindocs` :mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ... * The admindocs now allows esoteric setups where :setting:`ROOT_URLCONF` is not
a string.
:mod:`django.contrib.auth` :mod:`django.contrib.auth`
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -2,6 +2,7 @@ import sys
import unittest import unittest
from django.conf import settings from django.conf import settings
from django.contrib import admin
from django.contrib.admindocs import utils, views from django.contrib.admindocs import utils, views
from django.contrib.admindocs.views import get_return_data_type, simplify_regex from django.contrib.admindocs.views import get_return_data_type, simplify_regex
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
@ -9,7 +10,8 @@ from django.db import models
from django.db.models import fields from django.db.models import fields
from django.test import SimpleTestCase, modify_settings, override_settings from django.test import SimpleTestCase, modify_settings, override_settings
from django.test.utils import captured_stderr from django.test.utils import captured_stderr
from django.urls import reverse from django.urls import include, path, reverse
from django.utils.functional import SimpleLazyObject
from .models import Company, Person from .models import Company, Person
from .tests import AdminDocsTestCase, TestDataMixin from .tests import AdminDocsTestCase, TestDataMixin
@ -136,6 +138,21 @@ class AdminDocViewTests(TestDataMixin, AdminDocsTestCase):
response = self.client.get(reverse('django-admindocs-views-index')) response = self.client.get(reverse('django-admindocs-views-index'))
self.assertContains(response, 'View documentation') self.assertContains(response, 'View documentation')
def test_callable_urlconf(self):
"""
Index view should correctly resolve view patterns when ROOT_URLCONF is
not a string.
"""
def urlpatterns():
return (
path('admin/doc/', include('django.contrib.admindocs.urls')),
path('admin/', admin.site.urls),
)
with self.settings(ROOT_URLCONF=SimpleLazyObject(urlpatterns)):
response = self.client.get(reverse('django-admindocs-views-index'))
self.assertEqual(response.status_code, 200)
@override_settings(TEMPLATES=[{ @override_settings(TEMPLATES=[{
'NAME': 'ONE', 'NAME': 'ONE',