我正在使用react-redux和django创建一个音乐web应用程序,我有一个返回单个播放列表的函数,它返回html响应,而不是json,我知道当这两个应用程序分开时,它可以正常工作,但当iam使用django routs作为我的主要路由,并且只使用下面这行代码就像一个接口一样反应时:
urlpatterns += re_path(r'', TemplateView.as_view(template_name='index.html')),下面是我的函数
**django**
class getplaylist(APIView):
renderer_classes = [JSONRenderer]
def get(self,request):
id=request.META.get('HTTP_ID',None)
plst=playlist.objects.get(id=id)
return Response(plst)
**react**
export const getPlaylist=(id)=>{
return dispatch=>{
dispatch(getPlaylistStart())
axios.get('http://127.0.0.1:8000/songs/getplaylist/',{headers:{ 'id':id}})
.then(res=>{
console.log(res);
dispatch(getPlaylistSuccess(res.data))
})
}
}这是我的回答
playlist(pin):"<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>Songs App</title><link href="/static/css/2.3327982c.chunk.css" rel="stylesheet"><link href="/static/css/main.18cf81d6.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,f,l=r[0],i=r[1],a=r[2],c=0,s=[];c<l.length;c++)f=l[c],Object.prototype.hasOwnProperty.call(o,f)&&o[f]&&s.push(o[f][0]),o[f]=0;for(n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n]);for(p&&p(r);s.length;)s.shift()();return u.push.apply(u,a||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,l=1;l<t.length;l++){var i=t[l];0!==o[i]&&(n=!1)}n&&(u.splice(r--,1),e=f(f.s=t[0]))}return e}var n={},o={1:0},u=[];function f(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,f),t.l=!0,t.exports}f.m=e,f.c=n,f.d=function(e,r,t){f.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},f.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},f.t=function(e,r){if(1&r&&(e=f(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(f.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)f.d(t,n,function(r){return e[r]}.bind(null,n));return t},f.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return f.d(r,"a",r),r},f.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},f.p="/";var l=this.webpackJsonpfrontend=this.webpackJsonpfrontend||[],i=l.push.bind(l);l.push=r,l=l.slice();for(var a=0;a<l.length;a++)r(l[a]);var p=i;t()}([])</script><script src="/static/js/2.25085612.chunk.js"></script><script src="/static/js/main.dc42ca8e.chunk.js"></script></body></html>"发布于 2020-09-27 03:11:36
你的问题还不太清楚,就像你如何在django中使用reactJS一样。
如果你的ReactJS和Django应用程序同时运行在同一台服务器上,那么你必须使用正则表达式的url模式。
另外,在浏览器中测试他的url http://127.0.0.1:8000/songs/getplaylist/,而不是ajax请求。
如果你很好地看到了你的端点,那就意味着你的url/路由器模式可以正常运行了。如果没有,请将您的路由更新为使用regex。
https://stackoverflow.com/questions/64081490
复制相似问题