0

I have an app that is going to display some information about people in my group. I'm getting a NoReverseMatch error when trying to use the url tag in my index.html. If I do not use the url tag, but specify the root, I do not receive the error.

The error says:

NoReverseMatch at / Reverse for 'specialist' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$(?P[0-9]+)/']

Here is are the urls.py files.

From the main wi_tech urls.py:

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    url(r'^$', include('person.urls')),
    url(r'^tech/', include('person.urls')),
    url(r'^admin/', admin.site.urls),
]

From the 'person' app urls.py:

from django.conf.urls import url
from . import views
app_name = 'person'
urlpatterns = [
     url(r'^$', views.IndexView.as_view(), name='index'),
     url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='specialist'),
]

My views.py file looks like this:

from django.views import generic
from .models import Specialist
class IndexView(generic.ListView):
    template_name = 'person/index.html'
    context_object_name = 'person_list'

    def get_queryset(self):
        """Return all specialists"""
        return Specialist.objects.order_by('id')
class DetailView(generic.DetailView):
    model = Specialist
    template_name = 'person/detail.html'

And my index.html page looks like this:

{% if person_list %}
    <ul>
    {% for specialist in person_list %}
        <li><a href="{% url 'person:specialist' specialist.id %}"> {{ specialist }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No specialists are available.</p>
{% endif %}

If I change my tag in index.html to this, it works:

<li><a href="/tech/{{specialist.id}}"> {{ specialist }}</a></li>

Obviously this isn't an ideal situation in case the web root ever changes. I've reviewed a lot of SO questions on this, and nothing seems to match. I think the issue is the "$" in the beginning of the regex, but I don't see where that's coming from.

Specifically, I used this link as a really good reference point, but came up empty looking through my code. what is NoReverseMatch and how do i fix it

Clearly there's something I'm missing.

Community
  • 1
  • 1
  • Have you tried the following change to include: `url(r'^tech/', include('person.urls', namespace='person')),`? – user2390182 Nov 16 '16 at 23:35
  • 1
    Why do you include `person.urls` twice? The first include uses `^$`, which causes the error you're seeing. – knbk Nov 16 '16 at 23:55
  • @schwobaseggl `namespace` defaults to the `app_name` used, so that would have absolutely no effect. – knbk Nov 16 '16 at 23:56
  • @knbk - That was definitely my problem. I need to figure out another solution for the unadorned url (the ^$ usecase). Removing that line allowed the reverse match to work. – Rob Peterson Nov 17 '16 at 01:53

2 Answers2

0

Could it be the additional / at the end of the URL?

url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='specialist')

The one after your primary key modifier. When you click the URL (with the (% url ':' model.name %)) what URL comes up in your browser?

0

I ended up scrapping this implementation, and going with a flatter structure whereby all models, views and templates are in the same application. I have not had this problem in the new design.

  • Please have a look at [Can I answer my own question?](http://stackoverflow.com/help/self-answer) and come back two days later and check as answered. – help-info.de Feb 01 '17 at 19:04