Django url config with multiple urls matching
<p>in one of our Django applications we have defined multiple urls for views.</p>

<p>The first URL matches a general feature with pk and a second match group. 
The second URL matches a subfeature with pk.</p>

<p>Between this two urls more urls are defined, so it is not easy to see them all at once. Or, for example, the subfeature would have its own url.py.</p>

<pre><code># old urls.py
url(r'^(?P&lt;pk&gt;\d+)/', views.b),
url(r'^subfeature/', views.a),
</code></pre>

<p>After some time letters are also allowed in pk, so we now have to change <strong>\d+</strong> to <strong>[^/]+</strong>.</p>

<pre><code># new urls.py
url(r'^(?P&lt;pk&gt;[^/]+)/', views.b),
url(r'^subfeature/', views.a),
</code></pre>

<p>Now the subfeature breaks because the url is not correctly matched, 'subfeature' is matched as <strong>pk</strong> in the first url.</p>

<p>How to avoid breaking other urls when changing a url regex?</p>

<p>There's no general answer to that. Any change that makes an url more generic may break other urls that follow it.</p>

<p>In this case, you can swap the urls so <code>subfeature/</code> will match the subfeature url, and any other url will fall through and match <code>views.b</code>:</p>

<pre><code>url(r'^subfeature/', views.a),
url(r'^(?P&lt;pk&gt;[^/]+)/', views.b),
</code></pre>

python django