首页 > 解决方案 > 错误的响应格式 django-rest-framework

问题描述

我使用 react-redux 和 django 创建了一个音乐网络应用程序,我有返回单个播放列表的功能,它返回 html 响应而不是 json,知道当两个应用程序分开时它可以正常工作,但是当我使用 django 路由作为我的主要路由并做出反应时像只使用这一行的界面:

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>"

标签: reactjsdjangodjango-rest-frameworkreact-redux

解决方案


您的问题还不太清楚,就像您如何将 reactJS 与 django 一起使用。

如果您的 ReactJS 和 Django 应用程序在同一台服务器上一起运行,那么您必须使用正则表达式以 url 模式进行操作。

http://127.0.0.1:8000/songs/getplaylist/还要在浏览器中测试他的 url而不是 ajax 请求。

如果你能很好地看到你的端点,那就意味着你的 url/router 模式很好。如果没有,请使用正则表达式更新您的路线。


推荐阅读