3

I recently upgraded from Django 1.7 to 1.9, and having some trouble converting the urls with includes.

I get the error message: AttributeError: 'str' object has no attribute 'regex'

urls.py files below:

myapp/urls.py:

from django.conf.urls import url

from myapp.views import IndexView

#app_name = 'myapp'

urlpatterns = [
  url(r'^$', IndexView.as_view(), name='index'),
]

mysite/urls.py:

from django.conf.urls import url, include

urlpatterns = [
  url(r'^myapp/', include('myapp.urls')),

  # Have also tried the following combinations
  #url(r'^myapp/', include('myapp.urls', 'myapp')),
  #url(r'^myapp/', include('myapp.urls'), name='myapp'),

  # This one works
  url(r'^admin/', admin.site.urls),
]

Full stack trace:

Traceback (most recent call last):
  File "mysite/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/management/base.py", line 398, in execute
    self.check()
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/checks/urls.py", line 10, in check_url_config
    return check_resolver(resolver)
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/checks/urls.py", line 23, in check_resolver
    warnings.extend(check_resolver(pattern))
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/checks/urls.py", line 27, in check_resolver
    warnings.extend(check_pattern_startswith_slash(pattern))
  File "lib/python3.5/site-packages/Django-1.9.1-py3.5.egg/django/core/checks/urls.py", line 63, in check_pattern_startswith_slash
    regex_pattern = pattern.regex.pattern
AttributeError: 'str' object has no attribute 'regex'
2
  • myhost.tld/myapp Commented Mar 29, 2016 at 15:41
  • The stack trace above came from running on the command line, but I do get the same error trying to launch the test server. Commented Mar 29, 2016 at 15:44

3 Answers 3

2

Shouldn't you import include as well here from django.conf.urls import url, include ? I mean instead of having import, you should have include, I think.

Sign up to request clarification or add additional context in comments.

Comments

2

I had a block of code commented out as a string in myapp/urls.py which was causing the issue:

from django.conf.urls import url

from myapp.views import IndexView

#app_name = 'myapp'

urlpatterns = [
  url(r'^$', IndexView.as_view(), name='index'),

  '''

  ...some old code from the Django 1.7.6 config

  '''
]

Django is attempting to read the string as a url object, which was causing the error.

Removing the string fixed the problem.

Comments

0

try using

from django.conf.urls import url, include

instead

from django.conf.urls import url, import

1 Comment

That was a typo from when I copied in the text. Fixed it above, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.