Django url reverse error

540 Views Asked by At

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

1

There are 1 best solutions below

3
On

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'),
]