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>As a general rule, you need to take the order into consideration when defining urlpatterns.</p>

<p>The thing is django will try to find the match by trying to fit a url into every pattern and will try to get to the views whenever it finds the first url matching.</p>

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

<p>Here, it will first match the url with the first pattern where there is a variable initially and will try to put "subfeature" as pk and will tell you that it could not find any object with id="subfeature" (one pattern tried)</p>

<p>So yes, go with solution of user knbk. reverse the order and keep this in mind.</p>

python django