From 7b5aa20a281d1df27a9d94dd591845838a3cc804 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sun, 8 Feb 2015 13:44:07 -0500 Subject: [PATCH] [1.8.x] Fixed #17716 -- Prevented include('...', app_name='...') without a namespace. Backport of 27eeb64a96ec6d3a05714ab6319b500a935706fb from master --- django/conf/urls/__init__.py | 3 +++ tests/urlpatterns_reverse/tests.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/django/conf/urls/__init__.py b/django/conf/urls/__init__.py index 094b4cb3e8..6ed54930c4 100644 --- a/django/conf/urls/__init__.py +++ b/django/conf/urls/__init__.py @@ -17,6 +17,9 @@ handler500 = 'django.views.defaults.server_error' def include(arg, namespace=None, app_name=None): + if app_name and not namespace: + raise ValueError('Must specify a namespace if specifying app_name.') + if isinstance(arg, tuple): # callable returning a namespace hint if namespace: diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index 8ca55b9353..7c8c17a928 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -10,6 +10,7 @@ import unittest from admin_scripts.tests import AdminScriptTestCase from django.conf import settings +from django.conf.urls import include from django.contrib.auth.models import User from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist from django.core.urlresolvers import ( @@ -20,7 +21,9 @@ from django.http import ( HttpRequest, HttpResponsePermanentRedirect, HttpResponseRedirect, ) from django.shortcuts import redirect -from django.test import TestCase, ignore_warnings, override_settings +from django.test import ( + SimpleTestCase, TestCase, ignore_warnings, override_settings, +) from django.utils import six from django.utils.deprecation import RemovedInDjango20Warning @@ -399,7 +402,7 @@ class ReverseShortcutTests(TestCase): @ignore_warnings(category=RemovedInDjango20Warning) def test_reverse_by_path_nested(self): # Views that are added to urlpatterns using include() should be - # reversible by doted path. + # reversible by dotted path. self.assertEqual(reverse('urlpatterns_reverse.views.nested_view'), '/includes/nested_path/') def test_redirect_view_object(self): @@ -749,3 +752,10 @@ class ViewLoadingTests(TestCase): # swallow it. self.assertRaises(AttributeError, get_callable, 'urlpatterns_reverse.views_broken.i_am_broken') + + +class IncludeTests(SimpleTestCase): + def test_include_app_name_but_no_namespace(self): + msg = "Must specify a namespace if specifying app_name." + with self.assertRaisesMessage(ValueError, msg): + include('urls', app_name='bar')