[Django]-Django NoReverseMatch Exception for regular expression matching 2 routes

2๐Ÿ‘

โœ…

I think django is interpreting (statics|analytics) as a pattern for variable. I suggest as follow:

  1. define 2 different url to the same view:

    url(r'^v1/statistics/stats_bar$', views.stats_bar, name='stats_bar_statistics')
    url(r'^v1/analytics/stats_bar$', views.stats_bar, name='stats_bar_analytics')
    
    reverse('status_bar_statistics') # usage
    

    and inside the view test which url has been used:

    if 'analytics' in request.path:
        # do something
    elif 'statistics' in request.path:
        # do otherthing
    
  2. you define one url with parameter:

    url(r'^v1/ย (?P<type>(statistics|analytics))/stats_bar$', views.stats_bar, name='stats_bar')
    
    reverse('status_bar', kwargs={'type': 'statistics'}) # usage
    
๐Ÿ‘คDhia

1๐Ÿ‘

๐Ÿ‘คmarkzz

Leave a comment