0

iam creating a music web app using react-redux and django and i have function that returns single playlist it returns html response instead of json knowing that its working right when the two apps are seperated but when iam using django routs as my main routs and react like an interface only using this line:

urlpatterns += re_path(r'', TemplateView.as_view(template_name='index.html')),

here is my function

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

and this is the response

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>"
2
  • 1
    Have you tried removing the trailing slash? http://127.0.0.1:8000/songs/getplaylist Commented Sep 26, 2020 at 19:03
  • you are right that was the problem Commented Sep 27, 2020 at 10:40

1 Answer 1

1

Your problem not too clear yet like how you using reactJS with django.

If your ReactJS and Django app running together in same server, then you have to something in url pattern with regex.

Also test his url http://127.0.0.1:8000/songs/getplaylist/ in your browser instead of ajax request.

If you see your endpoint well, that is mean your url/router pattern is good to go. if not, please update your route to with regex.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.