4

I have a url configuration of this sort

urlpatterns = [
    url(r'webhook/', include('foward_bot.telegram_API.urls', namespace='api_webhook'), name='webhook'),
]

In telegram_API.urls I have

urlpatterns = [
    url(r'^(?P<token>[-_:a-zA-Z0-9]+)/$', TelegramView.as_view(), name='api_webhook'),
]

When I try to access this url with reverse in this manner

webhook = reverse('webhook', args={instance.token})

I get the error:

`Reverse for 'webhook' with arguments '(u'297704876:AAHiEy-slaktdaSMJfZtcnoDC-4HQYYDNOs',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []`

I have tried different variations like

webhook = reverse('webhook', kwargs={'token': instance.token}),
webhook = reverse('webhook:token', kwargs={'token': instance.token}),

But I always similar NoReverseMatch errors

Ken
  • 179
  • 10
  • Just out of curiosity, do you have better luck with the regex - `^(?P[[-_:\w\d]+)/$`? – Sayse Dec 23 '16 at 09:25
  • @Sayse not at all – Ken Dec 23 '16 at 09:49
  • @Sayse I have tried many options including the ones you suggested, which one in particular should I update the question with? – Ken Dec 23 '16 at 10:05
  • Mainly the `reverse('api_webhook:api_webhook'` since that is what appeared to me like the most likely issue – Sayse Dec 23 '16 at 10:07
  • As one last thing to try though, just set the `webhook` to the string that reverse would create - `'/webhook/297704876:AAHiEy-slaktdaSMJfZtcnoDC-4HQYYDNOs/'`. This would at least make it clear if the error is with the url regex or with the reversing – Sayse Dec 23 '16 at 10:09
  • I tried that, but when a request arrives at the webhook, it complains that such url does not exist – Ken Dec 23 '16 at 10:18
  • Right so that says that the issue is not with the reverse, that leaves two options, if your url pattern is shown in the list of patterns tried, then it is a problem with matching the regex. If it isn't in the list of patterns tried then its most likely a problem with your `INSTALLED_APPS` setting – Sayse Dec 23 '16 at 10:20
  • it's in the url configuration, my INSTALLED_APPS is fine. It was working until I decided to refactor the code. – Ken Dec 23 '16 at 10:22
  • Replace webhook to api_webhook webhook = reverse('api_webhook', args={instance.token}) – Neeraj Kumar Dec 23 '16 at 10:42
  • @NeErAjKuMaR already did, didn't help – Ken Dec 23 '16 at 13:27

1 Answers1

1

Reverse the url

As you have defined a namespace for the webhook, so when you need to reverse the url with the view name, you need to specify the namespace.

reverse('api_webhook:api_webhook', kwargs={'token': instance.token})

or

reverse('api_webhook:api_webhook', args=(instance.token,))

Some improvement to your url conf:

Here are some additional pointers on your urls.conf based on my experience.

urlpatterns = [
    url(r'^webhook/', include('foward_bot.telegram_API.urls', namespace='api_webhook'), name='webhook'),
]

urlpatterns = [
    url(r'(?P<token>[-_:a-zA-Z0-9]+)/$', TelegramView.as_view(), name='api_webhook'),
]
2ps
  • 15,099
  • 2
  • 27
  • 47
Enix
  • 4,415
  • 1
  • 24
  • 37
  • @ken i have tested the code above and it does work, do you make any changes to the code after you create this post? – Enix Dec 25 '16 at 03:40
  • Of course I have made changes while trying to solve the problem. But I have tried it exactly that way and it didn't work. Always says that `api_webhook is not a registered namespace`. I have not been able to solve it till now. It's confusing because in the django documentation they use `:` which I aslo tried but did not work too – Ken Dec 25 '16 at 17:17
  • I discovered the problem - the apps were nested, so I moved the include to the root url file and that solved the problem – Ken Dec 26 '16 at 18:47