3

I have a project that uses React in frontend and Django as backend.

Also I use react router in my project and the code looks like this:

<BrowserRouter>
    <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/teachers" exact component={Teachers} />
        <Route path="/courses" exact component={Courses}/>
        <Route path="/about" exact component={About} />
        <Route path="/posts" exact component={Posts} />
    </Switch>
</BrowserRouter>

In django my urls file looks like this:

urlpatterns = [
    path('', views.index, name="index"),
]

The problem is that whenever I try to navigate to the page from react router I get 404 error from django.

1 Answer 1

8

Dude I am having the same problem. From I have researched the answer is actually to throw a catch all at the end of your urls in django. I can only find the old way of doing it.

url(r'^(%s)?$' % '|'.join(routes), TemplateView.as_view(template_name='index.html'))

I am not sure how to do it with path but I had found it earlier. It might be

re_path(r'.*', views.index)

Just let me know if that works so I can go home and use it :)

Sign up to request clarification or add additional context in comments.

2 Comments

I have used re_path and it worked pretty well! Thanks a lot dawg!
Nice, I just got my first answer ever! Thank you sir!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.