简体   繁体   中英

Django reverse url found with arguments but fails with keywords

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, ie {% url loads:dataPoints ... %} (it fails)
- tried different regex patterns, eg 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({})
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>

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM