URL template tag causing NoReverseMatch when URL name from an include is used
<p>With the following definitions:</p>

<p><strong>urls.py</strong></p>

<pre><code>urlpatterns = [
    url(r'^admin/', admin.site.urls),

    # Homepage
    url(r'^$', projects.homepage, name='homepage'),
    url(r'^create_new_project/$', projects.create_new_project),

    # Project page
    url(r'^(?P&lt;project_path&gt;\w+)/', include('projects.urls')),        
]
</code></pre>

<p><strong>projects/urls.py</strong></p>

<pre><code>from . import views as projects
from gpuz import views as gpuz  # gpuz as a sample tool
from models import views as models

urlpatterns = [
    url(r'^$', projects.projectpage, name='project-page'),
    url(r'^gpuz/$', gpuz.page),
    url(r'^settings/$', models.settings_new),
]
</code></pre>

<p>I am able to resolve URLs such as <code>localhost/myproject/</code> and <code>localhost/myproject/gpuz/</code> but generating the URLs through templates gives me NoReverseMatch errors.</p>

<p>Example:</p>

<p><strong>base.html</strong></p>

<pre><code>&lt;div class="navbar-collapse collapse "&gt;
    &lt;ul id="menu-top" class="nav navbar-nav navbar-right"&gt;
        &lt;li&gt;&lt;a href="{% url 'homepage' %}" class="navbar-btn-home"&gt;Home&lt;/a&gt;&lt;/li&gt;

        {% if project %}
            &lt;!-- NoReverseMatch --&gt;
            &lt;li&gt;&lt;a href="{% url 'project-page' %}"&gt;{{ project.name }}&lt;/a&gt;&lt;/li&gt;
        {% endif %}

        {% if project_tools %}
            {% for tool in project_tools %}
                &lt;!-- Expected URL: /myproject/gpuz/ --&gt;
                &lt;li&gt;&lt;a href="{% url 'project-page' tool.name %}" class="navbar-btn-{{tool.name}}"&gt;{{ tool.name }}&lt;/a&gt;&lt;/li&gt;
            {% endfor %}

            &lt;li&gt;&lt;a href="/settings" class="navbar-btn-settings"&gt;Settings&lt;/a&gt;&lt;/li&gt;
        {% endif %}
    &lt;/ul&gt;
&lt;/div&gt;
</code></pre>

<p>I am following the example here: <a href="https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#url" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#url</a> but it doesn't seem to be working for me and I can't figure out what's wrong. I am not using <code>reverse()</code> anywhere in my code.</p>

<p>Error message:</p>

<p><strong>NoReverseMatch</strong></p>

<pre><code>{% if project %}
    &lt;li&gt;&lt;a href="{% url 'project-page' %}"&gt;{{ project.name }}&lt;/a&gt;&lt;/li&gt;
{% endif %}

Exception Type: NoReverseMatch
Exception Value: Reverse for 'project-page' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
</code></pre>

<p><strong>Temporary workaround</strong></p>

<p>I've been spending too much time trying to fix this so my current workaround is to chain variables to form a pseudo hard-coded URL e.g. <code>href="/{{ project.url_path }}/{{ tool.name }}/"</code>. </p>

<pre><code>url(r'^(?P&lt;project_path&gt;\w+)/', include('projects.urls'))
</code></pre>

<p>if you put this url in main urls.py you need to pass the project path from the Html page other wise it will raise the exception like no reverse match so right way is  </p>

<p>Main urls.py</p>

<pre><code>url(r'^$', include('projects.urls')), 
</code></pre>

<p>projects urls.py </p>

<p>add the project path url ( note: pass the project path from the template like <code>{% url 'project-page' project.path %}</code>
)</p>

python django django-urls