Excluding specific routes in Django with React Router?

I am currently building React with a Django REST backend. I have come to this little problem that I cannot get over, related to routing.

Here is my file urls.py

.

urlpatterns = [
    url(r'^api/', include(router.urls)),
    url(r'^admin/', admin.site.urls),
    url(r'^djangojs/', include('djangojs.urls')),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^$', TemplateView.as_view(template_name='exampleapp/itworks.html')),
    url(r'^(?:.*)/?$', TemplateView.as_view(template_name='exampleapp/itworks.html')),
]

      

Using this, it allows for responsive router operation on the front panel. For example, if I wanted to navigate to 127.0.0.1:8000/mentors

then it will take me to the page I set up for React Router.

However, because of this, executing the APIs in the frontend also returns the react page rather than the API endpoint. So whenever I remove the last line in the code above:, url(r'^(?:.*)/?$', TemplateView.as_view(template_name='exampleapp/itworks.html')),

it gets the API returned in JSON format successfully. The problem is that when I try to navigate to links, it will return a Django 404 page, not a React page set in React Router.

Is there anyway I can get the best of both worlds?

+4


source to share





All Articles