diff --git a/django/utils/html.py b/django/utils/html.py
index ce886efba7..a0ad5f3d00 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -141,7 +141,6 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x
safe_input = isinstance(text, SafeData)
words = word_split_re.split(force_unicode(text))
- nofollow_attr = nofollow and ' rel="nofollow"' or ''
for i, word in enumerate(words):
match = None
if '.' in word or '@' in word or ':' in word:
@@ -150,6 +149,7 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
lead, middle, trail = match.groups()
# Make URL we want to point to.
url = None
+ nofollow_attr = ' rel="nofollow"' if nofollow else ''
if middle.startswith('http://') or middle.startswith('https://'):
url = smart_urlquote(middle)
elif middle.startswith('www.') or ('@' not in middle and \
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
index 515840d87e..2c7cdb17a6 100644
--- a/tests/regressiontests/defaultfilters/tests.py
+++ b/tests/regressiontests/defaultfilters/tests.py
@@ -253,6 +253,11 @@ class DefaultFiltersTests(TestCase):
u''
u'http://en.wikipedia.org/wiki/Café')
+ # Check urlize adds nofollow properly - see #12183
+ self.assertEqual(urlize('foo@bar.com or www.bar.com'),
+ u'foo@bar.com or '
+ u'www.bar.com')
+
# Check urlize handles IDN correctly - see #13704
self.assertEqual(urlize('http://c✶.ws'),
u'http://c✶.ws')