slick_reaper
slick_reaper

Reputation: 192

URL Routing trouble in React + Django

I have the following urlpatterns configured for my Django app. This rule will route to http://localhost:8000/profile/.

urlpatterns = [
    path("profile/", test_frontend_views.profile),
]

The views code:

def profile(request):
    return render(request, 'frontend/Profile_temp_master.html')

Within my React components that are dynamically creating HTML for the template, I am making requests to my Django backend API with the following scheme:

fetch("api/profile", requestOptions).then(..).then(..)

However, rather than this request routing to http://localhost:8000/api/profile/, it is routing to http://localhost:8000/profile/api/profile/ which is not a valid endpoint and returns and error. How can I solve this so that the URL routes to the correct endpoint?

Upvotes: 0

Views: 219

Answers (1)

trixn
trixn

Reputation: 16309

You need to do

fetch("/api/profile", requestOptions).then(..).then(..)

with a / at the start to make it absolute. Otherwise it will be relative to the current url.

Upvotes: 1

Related Questions