为什么我的POST请求会导致404错误?

时间:2018-11-23 20:09:35

标签: reactjs django-rest-framework

我一直在努力将我的react应用中的发布请求提交给我的Django rest框架api:继续出现此错误:

  

找不到404无法发布/ api / tokens /

我正在使用redux处理状态并将数据发布到在localhost:8000上运行的django api中:

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

在webpack.donf.dev.js中:

const publicPath = 'http://localhost:3000/';

const publicUrl = 'http://localhost:3000/';

这是我的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")),

]

和端点。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()),
]

不确定这是否足够信息,因为我是React.js的新手

更新

这是我的设置。py

WEBPACK_LOADER = {
    'DEFAULT': {
            'BUNDLE_DIR_NAME': 'bundles/',
            'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.dev.json'),
        }
}

然后使用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"}]}}

0 个答案:

没有答案