AttributeError: 'str' object has no attribute 'regex' django 1.9

Hannah Wight picture Hannah Wight · Jan 18, 2016 · Viewed 8.1k times · Source

I am working with django 1.9 and I am currently coding - in Windows Command Prompt - python manage.py makemigrations and the error:

AttributeError: 'str' object has no attribute 'regex'

I have tried coding:

url(r'^$', 'firstsite.module.views.start', name="home"),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'),
url(r'^signup/$', 'exam.views.signup', name='signup'),
url(r'^signup/submit/$', 'exam.views.signup_submit', name='signup_submit')

in urls.py and the error is keeps coming up.

This is my first time coding in django, so my expertise is very limited. Thank you in advance.

This is the whole urls.py:

from django.conf.urls import patterns, include, url
import django
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = patterns('',
  # Examples:
  # url(r'^$', 'firstsite.views.home', name='home'),
  # url(r'^firstsite/', include('firstsite.foo.urls')),

  # Uncomment the admin/doc line below to enable admin documentation:
  # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

  # Uncomment the next line to enable the admin:
  #url(r'^admin/', include(admin.site.urls)),

  django.conf.urls.handler400,

  url(r'^$', 'firstsite.module.views.start', name="home"),
  url(r'^admin/', include(admin.site.urls)),
  url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'),
  url(r'^signup/$', 'exam.views.signup', name='signup'),
  url(r'^signup/submit/$', 'exam.views.signup_submit', name='signup_submit'),
  )

Answer

i41 picture i41 · Mar 8, 2016

Also make sure to remove the beginning empty url pattern--can be overlooked when migrating your urls.

urlpatterns = ['', # <== this blank element ('') produces the error.
    ...
]

tl;dr

For the curious, I found this out by adding a warning to the check_pattern_startswith_slash method in the django.core.checks.urls module:

def check_pattern_startswith_slash(pattern):
    """
    Check that the pattern does not begin with a forward slash.
    """
    if not hasattr(pattern, 'regex'):
        warning = Warning(
            "Invalid pattern '%s'" % pattern,
            id="urls.W002",
        )
        return [warning]

And, sure enough, I got a bunch of warnings like this:

?: (urls.W002) Invalid pattern ''