Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use react router in django project?

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.

like image 668
Dils Matchanov Avatar asked Jan 26 '26 16:01

Dils Matchanov


1 Answers

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 :)

like image 108
lakerskill Avatar answered Jan 29 '26 07:01

lakerskill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!