Why does my POST reques result in a 404 error?
I've been struggling to submit a post request from my react app to my Django rest framework api: keep on getting this error:
404 not found Cannot POST /api/tokens/
I am using redux to handle state and post the data to my django api running on localhost:8000
:
export const addToken = (company_name, name, address, supply, holders) => {
return dispatch => {
let headers = {"Content-Type": "application/json"};
let body = JSON.stringify({company_name, name, address, supply, holders,});
debugger
return fetch("/api/tokens/", {headers, method: "POST", body})
.then(res => res.json())
.then(token => {
return dispatch({
type: 'ADD_TOKEN',
token
})
})
}
}
In webpack.donf.dev.js:
const publicPath = 'http://localhost:3000/';
const publicUrl = 'http://localhost:3000/';
and here is my url.py:
from contract import endpoints
urlpatterns = [
url(r'^api/', include(endpoints)),
path('admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls')),
url(r'^', TemplateView.as_view(template_name="index.html")),
]
and the endpoint.py
from contract import views
from .api import TokenViewSet
router = routers.DefaultRouter()
router.register('tokens', TokenViewSet, 'tokens')
urlpatterns = [
url("^", include(router.urls)),
path('tokens/', views.TokenList.as_view()),
path('tokens/<int:pk>/', views.TokenDetail.as_view()),
path('users/', views.UserList.as_view()),
path('users/<int:pk>/', views.UserDetail.as_view()),
]
Not sure if this is enough info as I am pretty new to React.js
UPDATE
Here is my settings.py
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.dev.json'),
}
}
then webpack-stats-dev.json:
{"status":"done","publicPath":"http://localhost:3000/","chunks":{"main":[{"name":"static/js/bundle.js","publicPath":"http://localhost:3000/static/js/bundle.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js"},{"name":"main.d66a743648ca15de12df.hot-update.js","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js"},{"name":"static/js/bundle.js.map","publicPath":"http://localhost:3000/static/js/bundle.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js.map"},{"name":"main.d66a743648ca15de12df.hot-update.js.map","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js.map"}]}}
reactjs django-rest-framework
add a comment |
I've been struggling to submit a post request from my react app to my Django rest framework api: keep on getting this error:
404 not found Cannot POST /api/tokens/
I am using redux to handle state and post the data to my django api running on localhost:8000
:
export const addToken = (company_name, name, address, supply, holders) => {
return dispatch => {
let headers = {"Content-Type": "application/json"};
let body = JSON.stringify({company_name, name, address, supply, holders,});
debugger
return fetch("/api/tokens/", {headers, method: "POST", body})
.then(res => res.json())
.then(token => {
return dispatch({
type: 'ADD_TOKEN',
token
})
})
}
}
In webpack.donf.dev.js:
const publicPath = 'http://localhost:3000/';
const publicUrl = 'http://localhost:3000/';
and here is my url.py:
from contract import endpoints
urlpatterns = [
url(r'^api/', include(endpoints)),
path('admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls')),
url(r'^', TemplateView.as_view(template_name="index.html")),
]
and the endpoint.py
from contract import views
from .api import TokenViewSet
router = routers.DefaultRouter()
router.register('tokens', TokenViewSet, 'tokens')
urlpatterns = [
url("^", include(router.urls)),
path('tokens/', views.TokenList.as_view()),
path('tokens/<int:pk>/', views.TokenDetail.as_view()),
path('users/', views.UserList.as_view()),
path('users/<int:pk>/', views.UserDetail.as_view()),
]
Not sure if this is enough info as I am pretty new to React.js
UPDATE
Here is my settings.py
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.dev.json'),
}
}
then webpack-stats-dev.json:
{"status":"done","publicPath":"http://localhost:3000/","chunks":{"main":[{"name":"static/js/bundle.js","publicPath":"http://localhost:3000/static/js/bundle.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js"},{"name":"main.d66a743648ca15de12df.hot-update.js","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js"},{"name":"static/js/bundle.js.map","publicPath":"http://localhost:3000/static/js/bundle.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js.map"},{"name":"main.d66a743648ca15de12df.hot-update.js.map","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js.map"}]}}
reactjs django-rest-framework
Where are you setting thefetch
base URL aslocalhost:8000
? To me it seems you are requesting your front-end dev server.
– enapupe
Nov 23 '18 at 20:13
That's what I thought too but I follow a tutorial at: v1k45.com/blog/modern-django-part-1-setting-up-django-and-react and he basically linked up django so that it listens to 3000. Here I updated my question
– Cyzanfar
Nov 23 '18 at 20:14
Then it seems you gotta hit your client/browser onlocalhost:8000
notlocalhost:3000
.. Because python/django is running on 8000 and "proxying" the front-end under it.
– enapupe
Nov 23 '18 at 20:16
Yeah makes sense man but the tutorial says otherwise...
– Cyzanfar
Nov 23 '18 at 20:31
add a comment |
I've been struggling to submit a post request from my react app to my Django rest framework api: keep on getting this error:
404 not found Cannot POST /api/tokens/
I am using redux to handle state and post the data to my django api running on localhost:8000
:
export const addToken = (company_name, name, address, supply, holders) => {
return dispatch => {
let headers = {"Content-Type": "application/json"};
let body = JSON.stringify({company_name, name, address, supply, holders,});
debugger
return fetch("/api/tokens/", {headers, method: "POST", body})
.then(res => res.json())
.then(token => {
return dispatch({
type: 'ADD_TOKEN',
token
})
})
}
}
In webpack.donf.dev.js:
const publicPath = 'http://localhost:3000/';
const publicUrl = 'http://localhost:3000/';
and here is my url.py:
from contract import endpoints
urlpatterns = [
url(r'^api/', include(endpoints)),
path('admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls')),
url(r'^', TemplateView.as_view(template_name="index.html")),
]
and the endpoint.py
from contract import views
from .api import TokenViewSet
router = routers.DefaultRouter()
router.register('tokens', TokenViewSet, 'tokens')
urlpatterns = [
url("^", include(router.urls)),
path('tokens/', views.TokenList.as_view()),
path('tokens/<int:pk>/', views.TokenDetail.as_view()),
path('users/', views.UserList.as_view()),
path('users/<int:pk>/', views.UserDetail.as_view()),
]
Not sure if this is enough info as I am pretty new to React.js
UPDATE
Here is my settings.py
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.dev.json'),
}
}
then webpack-stats-dev.json:
{"status":"done","publicPath":"http://localhost:3000/","chunks":{"main":[{"name":"static/js/bundle.js","publicPath":"http://localhost:3000/static/js/bundle.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js"},{"name":"main.d66a743648ca15de12df.hot-update.js","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js"},{"name":"static/js/bundle.js.map","publicPath":"http://localhost:3000/static/js/bundle.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js.map"},{"name":"main.d66a743648ca15de12df.hot-update.js.map","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js.map"}]}}
reactjs django-rest-framework
I've been struggling to submit a post request from my react app to my Django rest framework api: keep on getting this error:
404 not found Cannot POST /api/tokens/
I am using redux to handle state and post the data to my django api running on localhost:8000
:
export const addToken = (company_name, name, address, supply, holders) => {
return dispatch => {
let headers = {"Content-Type": "application/json"};
let body = JSON.stringify({company_name, name, address, supply, holders,});
debugger
return fetch("/api/tokens/", {headers, method: "POST", body})
.then(res => res.json())
.then(token => {
return dispatch({
type: 'ADD_TOKEN',
token
})
})
}
}
In webpack.donf.dev.js:
const publicPath = 'http://localhost:3000/';
const publicUrl = 'http://localhost:3000/';
and here is my url.py:
from contract import endpoints
urlpatterns = [
url(r'^api/', include(endpoints)),
path('admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls')),
url(r'^', TemplateView.as_view(template_name="index.html")),
]
and the endpoint.py
from contract import views
from .api import TokenViewSet
router = routers.DefaultRouter()
router.register('tokens', TokenViewSet, 'tokens')
urlpatterns = [
url("^", include(router.urls)),
path('tokens/', views.TokenList.as_view()),
path('tokens/<int:pk>/', views.TokenDetail.as_view()),
path('users/', views.UserList.as_view()),
path('users/<int:pk>/', views.UserDetail.as_view()),
]
Not sure if this is enough info as I am pretty new to React.js
UPDATE
Here is my settings.py
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.dev.json'),
}
}
then webpack-stats-dev.json:
{"status":"done","publicPath":"http://localhost:3000/","chunks":{"main":[{"name":"static/js/bundle.js","publicPath":"http://localhost:3000/static/js/bundle.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js"},{"name":"main.d66a743648ca15de12df.hot-update.js","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js"},{"name":"static/js/bundle.js.map","publicPath":"http://localhost:3000/static/js/bundle.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/static/js/bundle.js.map"},{"name":"main.d66a743648ca15de12df.hot-update.js.map","publicPath":"http://localhost:3000/main.d66a743648ca15de12df.hot-update.js.map","path":"/Users/cyrusghazanfar/Desktop/python-projects/smart-contract-distributor/vestvault/web_interface/dist/main.d66a743648ca15de12df.hot-update.js.map"}]}}
reactjs django-rest-framework
reactjs django-rest-framework
edited Nov 23 '18 at 20:26
isherwood
37.2k1082111
37.2k1082111
asked Nov 23 '18 at 20:09
CyzanfarCyzanfar
4,40032347
4,40032347
Where are you setting thefetch
base URL aslocalhost:8000
? To me it seems you are requesting your front-end dev server.
– enapupe
Nov 23 '18 at 20:13
That's what I thought too but I follow a tutorial at: v1k45.com/blog/modern-django-part-1-setting-up-django-and-react and he basically linked up django so that it listens to 3000. Here I updated my question
– Cyzanfar
Nov 23 '18 at 20:14
Then it seems you gotta hit your client/browser onlocalhost:8000
notlocalhost:3000
.. Because python/django is running on 8000 and "proxying" the front-end under it.
– enapupe
Nov 23 '18 at 20:16
Yeah makes sense man but the tutorial says otherwise...
– Cyzanfar
Nov 23 '18 at 20:31
add a comment |
Where are you setting thefetch
base URL aslocalhost:8000
? To me it seems you are requesting your front-end dev server.
– enapupe
Nov 23 '18 at 20:13
That's what I thought too but I follow a tutorial at: v1k45.com/blog/modern-django-part-1-setting-up-django-and-react and he basically linked up django so that it listens to 3000. Here I updated my question
– Cyzanfar
Nov 23 '18 at 20:14
Then it seems you gotta hit your client/browser onlocalhost:8000
notlocalhost:3000
.. Because python/django is running on 8000 and "proxying" the front-end under it.
– enapupe
Nov 23 '18 at 20:16
Yeah makes sense man but the tutorial says otherwise...
– Cyzanfar
Nov 23 '18 at 20:31
Where are you setting the
fetch
base URL as localhost:8000
? To me it seems you are requesting your front-end dev server.– enapupe
Nov 23 '18 at 20:13
Where are you setting the
fetch
base URL as localhost:8000
? To me it seems you are requesting your front-end dev server.– enapupe
Nov 23 '18 at 20:13
That's what I thought too but I follow a tutorial at: v1k45.com/blog/modern-django-part-1-setting-up-django-and-react and he basically linked up django so that it listens to 3000. Here I updated my question
– Cyzanfar
Nov 23 '18 at 20:14
That's what I thought too but I follow a tutorial at: v1k45.com/blog/modern-django-part-1-setting-up-django-and-react and he basically linked up django so that it listens to 3000. Here I updated my question
– Cyzanfar
Nov 23 '18 at 20:14
Then it seems you gotta hit your client/browser on
localhost:8000
not localhost:3000
.. Because python/django is running on 8000 and "proxying" the front-end under it.– enapupe
Nov 23 '18 at 20:16
Then it seems you gotta hit your client/browser on
localhost:8000
not localhost:3000
.. Because python/django is running on 8000 and "proxying" the front-end under it.– enapupe
Nov 23 '18 at 20:16
Yeah makes sense man but the tutorial says otherwise...
– Cyzanfar
Nov 23 '18 at 20:31
Yeah makes sense man but the tutorial says otherwise...
– Cyzanfar
Nov 23 '18 at 20:31
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452439%2fwhy-does-my-post-reques-result-in-a-404-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452439%2fwhy-does-my-post-reques-result-in-a-404-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where are you setting the
fetch
base URL aslocalhost:8000
? To me it seems you are requesting your front-end dev server.– enapupe
Nov 23 '18 at 20:13
That's what I thought too but I follow a tutorial at: v1k45.com/blog/modern-django-part-1-setting-up-django-and-react and he basically linked up django so that it listens to 3000. Here I updated my question
– Cyzanfar
Nov 23 '18 at 20:14
Then it seems you gotta hit your client/browser on
localhost:8000
notlocalhost:3000
.. Because python/django is running on 8000 and "proxying" the front-end under it.– enapupe
Nov 23 '18 at 20:16
Yeah makes sense man but the tutorial says otherwise...
– Cyzanfar
Nov 23 '18 at 20:31