1

I have this project using DRF...so I have sucessfully filtered the data that I wanted, but can't render a view as json to it.

This is my filter:

class ArticleFilteredList(generics.ListAPIView):
    serializer_class = ArticleSerializer

    def get_queryset(self):
        """
        This view should return a list of all the purchases for
        the user as determined by the username portion of the URL.
        """
        subject = self.kwargs['subject']
        return Article.objects.filter(subject__name=subject)

It filters Articles by Subject, and then returns a list of this Articles, but the response that I'm getting is this: This is the correct filtered article...

The problem is, I wan't to fetch the data using, some fetch API like: axios or fetch...It worked on this kind of Json structure: This is the structure that I wan't to render

Given the facts I need one of the following: either render the data as json like the second image, or find a way to fetch data from the first image, which is unknown to me, plus it fires this error with CORS:

    XMLHttpRequest cannot load http://localhost:8000/news/articles/POLITICS.  
 Redirect from 'http://localhost:8000/news/articles/POLITICS' to  
 'http://localhost:8000/news/articles/POLITICS/' has been blocked by CORS  
 policy: No 'Access-Control-Allow-Origin' header is present on the requested  
 resource. Origin 'http://localhost:5000' is therefore not allowed access. 

My settings.py allows it:

CORS_ORIGIN_WHITELIST = (
    'localhost',
    'localhost:5000',
    .
    .
    .
)

ALLOWED_HOSTS = ['localhost', ...,'http://localhost:5000']

So I'm not really sure what's happening...

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197

1 Answers1

0

This is not optimal, but this settings solved it:

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = False

I'm taking this by solved, if anyone have a sugestion, I'm happy to hear it :).