What is happening in this urlpatterns list of Python Django urls.py file?
<p>I have three versions of <code>urls.py</code> file.</p>

<p>Here are imports (shared between versions):</p>

<pre><code>from django.conf.urls.static import static
from django.conf import settings
from django.conf.urls import patterns, url

from main import views
</code></pre>

<p><strong>Version 1.</strong> Everything works fine here. No problems running <code>python2 manage.py runserver</code>.</p>

<pre><code>urlpatterns = patterns(
    url(r'^bio$', 'views.bio_view'),
)
</code></pre>

<p><strong>Version 2.</strong> Hmm I need some more urls though. Let's add them. No problems here either.</p>

<pre><code>urlpatterns = patterns(
    '',
    url(r'^$', views.index, name='index'),
    url(r'^bio$', 'views.bio_view'),
)
</code></pre>

<p><strong>Version 3.</strong> Wait a sec... What is <code>''</code> doing here? I don't actually need it. Let's remove it, shall we?</p>

<pre><code>urlpatterns = patterns(
    url(r'^$', views.index, name='index'),
    url(r'^bio$', 'views.bio_view'),
)
</code></pre>

<p>And here is the issue after running the <code>manage.py</code> server:</p>

<p>(Some of top <code>django</code> library calls ommitted)</p>

<pre><code>  File "/home/konrad/workspace/mydir/myproject/urls.py", line 20, in &lt;module&gt;
    url(r'^', include('main.urls')),
  File "/usr/lib/python2.7/site-packages/django/conf/urls/__init__.py",     line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in    import_module
    __import__(name)
  File "/home/konrad/workspace/mydir/myproject/urls.py", line 15, in &lt;module&gt;
    url(r'^bio$', 'views.bio_view'),
  File "/usr/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 91, in patterns
    t.add_prefix(prefix)
  File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 232, in add_prefix
    self._callback_str = prefix + '.' + self._callback_str
TypeError: unsupported operand type(s) for +: 'RegexURLPattern' and 'unicode'
</code></pre>

<p>So... Actually the question is about the <strong>Version 2.</strong> - why is it fixing the <strong>Version 3.</strong> error? And what is the error?</p>

<p>What is actually happening here?</p>

<p>I'm not sure why you're surprised; you removed a parameter, and things went wrong. (Your first version might well have "worked" when you ran the server, but I doubt you could actually get to the URL.)</p>

<p>You are using an old version of Django. In this version, <code>urlpatterns</code> must be defined with the result of the <code>patterns</code> function. The first parameter to that function is a prefix to apply to all view strings. Your prefix is empty, but that doesn't mean you can just remove it; your first URL is now being taken as the prefix parameter.</p>

<p>In recent versions it was recognised that this prefix is confusing and rarely used. As a result, the <code>patterns</code> function is removed and there is no prefix; the value of <code>urlpatterns</code> must now be a simple list. Additionally, views in urls must be callables, not strings.</p>

python django python-2.7 typeerror django-urls