1

This is yet another question regarding the error:

Reverse for 'dataPoints' with arguments '()' and keyword arguments '{u'loadID': 5}' not found. 1 pattern(s) tried: [u'loads/dataPoints/']

I've sifted through scads of related post but can't seem to figure out what's going on. The problem is this: in an app using url namespaces, a view template fails with the above error when I try to pass keyword arguments to the url, but works if I use position arguments.

This is what is giving me the above error:

Top-level urls.py:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    ...
    url(r'^loads/', include(sig.views.data_loads.urls_data_loads, namespace="loads")),
    url(r'^authentication/', include(sig.views.authentication.urls_authentication, namespace="authentication")),
    url(r'^account/', include(sig.views.account.urls_account, namespace="account")),
)

urls.py for "loads":

urlpatterns = patterns('',
    url(r'^dataPoints', views.DataPoints.as_view(), name='dataPoints')
)

template.html:

<a href="{% url 'loads:dataPoints' loadID=5 %}">points</a>

Then it says it can't find the reverse. Based on some of the related threads I've found, I've tried:
- trying the link without quotes, i.e. {% url loads:dataPoints ... %} (it fails)
- tried different regex patterns, e.g. url(r'^dataPoints(.)*) (still can't find reverse)

I can easily work around this by using positional arguments but it's really bugging me that I can't figure it out. I've used keyword args like this in apps before, and I'm wondering if something is screwy because I'm using url namespaces? Either that or, more likely, I'm doing something completely boneheaded.

EDIT: Adding code for DataPoints view:

class DataPoints(TemplateView):
    template_name = "data_loads/templates/dataPoints.html"
    def get(self, request):
        loadID = request.GET["loadID"]
        return self.render_to_response({})
Gadzooks34
  • 1,718
  • 2
  • 20
  • 29
  • are you overriding the default `__init__` for the view `DataPoint`? Can you post the code of the view? – brainless coder Jun 03 '14 at 16:46
  • No, I don't think I'm doing anything funky there. I edited the post to include the code for the DataPoints view, which is just a skeleton right now. – Gadzooks34 Jun 03 '14 at 19:17

3 Answers3

1
urlpatterns = patterns('',
    url(r'^dataPoints', views.DataPoints.as_view(), name='dataPoints')
)

Your modified url does not take any parameter as the one before takes loadID.

So the behavior you are seeing is correct.

To use kwargs, keep your url same and use the template as you are doing

<a href="{% url 'loads:dataPoints' loadID=5 %}">points</a>
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • The other url doesn't specify the argument in the pattern but r'^dataPoints' should match "dataPoints?load=5" should it not? I think it would only be a problem if I had r'^dataPoints$'. But regardless, I have tried permutations on this regex like the example I listed in the question, r'^dataPoints(.)*'. – Gadzooks34 Jun 03 '14 at 12:50
  • @Gadzooks34, No thats not correct. With `r'^dataPoints'` you are telling that there are no parameters. So `{%url ..%}` won't find it when you specify with kwargs. – Rohan Jun 03 '14 at 12:54
  • But what would that look like? "r'^dataPoints?loadID=[0-9]+$"? I've never seen anyone do this. (It also doesn't work). Is there some other way of indicating that you're expecting args in the url? – Gadzooks34 Jun 03 '14 at 13:15
  • @Gadzooks34, Django url parser does not parse arguments in url, ref: https://docs.djangoproject.com/en/dev/topics/http/urls/#what-the-urlconf-searches-against – Rohan Jun 03 '14 at 13:30
  • Right but isn't that another argument why my original pattern r'^dataPoints' should work? I've also tried things like r'^dataPoints/?$'. Would you mind posting what you think the correct regex should be? – Gadzooks34 Jun 03 '14 at 13:40
  • No, it isn't an argument for why that pattern should work. That pattern will match ANY url starting with 'dataPoints'. As Rohan has said, your amended pattern is not looking for 'loadID' and that is the reason you are receiving the error. See my separate answer for more detail. – hellsgate Jun 03 '14 at 14:36
1

I'm taking your comments on Rohans answer into account.

The pattern r'^dataPoints' will match any url that starts 'dataPoints', including dataPoints?load=5, but 'load' will be added to request.GET (so request.GET['load'] is '5'). This is the reason you have never seen anyone doing something like r'^dataPoints?loadID=[0-9]+$ as it is not how url patterns work. Rohans original answer here is correct, you only need to revert to the orginal pattern in urls.py for 'load'. You should try what he has suggested and accept his answer if the error goes away, which I'm sure it will.

hellsgate
  • 5,905
  • 5
  • 32
  • 47
  • Many thanks to both you and Rohan for your help. I completely agree with your comment. But, unless I'm missing something, Rohan's suggestion was to do exactly what I was originally doing (I have reverted to it and it still repeatably produces the error). My comment concerning the unusual regex was in response to his assertion that, "With r'^dataPoints' you are telling that there are no parameters..." which still confuses me. Since I've gotten consistent feedback that matches with my original understanding, I guess I need to start hunting for weird namespace collisions or something. – Gadzooks34 Jun 03 '14 at 15:07
1

OK - I knew it had to be something dumb on my part. I assumed that "keyword arguments" in the context of url dispatching meant GET parameters, not keyword args routed to the get() method. This is also why I was so confused by the comments mentioning a method signature. Ugh. Pretty dumb but hopefully this will help anyone else who makes the same mistake. Anyway, the final answer is that the line in my template file should have read:

<a href="{% url 'loads:dataPoints' %}?loadID=5">points</a>

The post that finally caused this to click in my head was this one. Thanks, everyone, for your help.

Community
  • 1
  • 1
Gadzooks34
  • 1,718
  • 2
  • 20
  • 29