Django writeback error

I have such a URL configuration

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 callback url this way

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

I get an error message:

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

I tried different options like

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

But I always go wrong NoReverseMatch

+4
source share
1 answer

Return url

How you defined the namespace for webhook, so when you need to change the URL with the name of the view, 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 on your conf url:

Here are some additional pointers to 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'),
]
+1
source

Source: https://habr.com/ru/post/1664652/


All Articles