Omar khaled
Omar khaled

Reputation: 61

how can I use react router with 'django'

I wanna use react as frontend with django as backend I tried

urlpatterns = [
    path('api/',include('api.urls')),
    path('admin/', admin.site.urls),
    path('',include('frontend.urls')),
]

frontend is the app contains react

frontend app urls

urlpatterns = [
    path('/',index)
]
def index(request):
    return render(request,'frontend/index.html')

i wanna use index.html any other route

Upvotes: 4

Views: 5774

Answers (2)

mogler.dev
mogler.dev

Reputation: 86

project/urls.py

urlpatterns = [
    path('api/',include('api.urls')), 
    path('admin/', admin.site.urls),
    path('', include('frontend.urls')), # This has to be the last item!
]

frontend/urls.py

#...
from django.urls import re_path
#...
urlpatterns = [
    re_path(r".*", index) # RegExpr: any character is correct
]

Upvotes: 6

knada
knada

Reputation: 344

Django routing and react routing don't have to mix. They are completely separate from each other. You get your API data from the django routes and then create you own routes on the front-end with react-router. Read this for some context: https://www.digitalocean.com/community/tutorials/build-a-to-do-application-using-django-and-react.

Upvotes: 0

Related Questions