Alex Grs
Alex Grs

Reputation: 3301

URL with args does not reverse

I have the following url:

urlpatterns += patterns('app_common.views_settings',
    url(r'([\w-]+)/(\d+)/settings/$', 'settings', name="configuration_homepage"),
    url(r'(?P<short_name>[\w-]+)/(?P<product_id>\d+)/settings/modify/(?P<sim_id>\d+)/$', 'modify_sim', name="modify_sim"),
)

urlpatterns += patterns('app_common.views_operator',
    url(r'^operator/$', 'choose_operator', name="choose_operator"),
    url(r'^(?P<short_name>[\w-]+)/project/$', 'choose_project', name="choose_project"),
    url(r'([\w-]+)/(\d+)/$', 'set_product', name="set_product"),
    url(r'^(?P<short_name>[\w-]+)/$', 'set_operator', name="set_operator"),
)

I tried to reverse configuration homepage using:

url = reverse('configuration_homepage', kwargs={short_name, product_id})
return HttpResponseRedirect(url)

Some times it works, but other times if failed with this issue (short_name=OCI and product_id=1)

Exception Type: NoReverseMatch
Exception Value:    Reverse for 'configuration_homepage' with arguments '(u'1', u'OCI')' and keyword arguments '{}' not found.

If you guys detect something wrong in my code fell free to tell me... I tried to give name to variable but urls are not found in that case.

Upvotes: 1

Views: 106

Answers (2)

Rohan
Rohan

Reputation: 53316

Use args instead of kwargs,

url = reverse('configuration_homepage', args=[short_name, product_id])

Upvotes: 1

Kimvais
Kimvais

Reputation: 39538

Your kwargs is wrong, you are passing a set() instead of a dict()

What you (probably) want is:

url = reverse('configuration_homepage',
              kwargs={short_name: short_name, product_id: product_id})

This is one of the many reasons why I prefer dict(a=1, b=2) over {a:1, b:2} when possible,

Upvotes: 1

Related Questions