简体   繁体   中英

How can i update a page without reloading with Ajax and Django Rest Framework?

In my Django project, i'm trying to create a page where some data is uploaded in real time, without reloading the whole page.

That data is retrieved from a database, so i created an API endpoint with Django Rest Framework, the problem is that i don't know how to go from here.

I already know that, to update the page, i'll need to use Ajax. But i don't know how to create the Ajax part. I think that i need to add a POST request in my template, but that's all i know for now. Can someone give me some direction on where to go from here? Any advice is appreciated

Basically the Ajax request should call the endpoint, which is http://127.0.0.1:8000/tst/ , and update the data every X (something between 1 and 5 seconds).

serializers.py

class tstSerializer(serializers.ModelSerializer):

    class Meta:
        model = tst
        fields = ('ticker', 'Price', )

    def create(self, validated_data):
        return tst.objects.create(**validated_data)

views.py

class tstList(generics.ListCreateAPIView):
    queryset = tst.objects.using('screener').all()
    serializer_class = tstSerializer


class tstDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = tst.objects.using('screener').all()
    serializer_class = tstSerializer

template.html

<h3>Here will be a table with the data uploaded in real time..</h3>

Problem solved, basically the collection i wanted to use is called tst . When i created the model tst in my Django project, instead of using the already existing collection called tst , it created a new collection called main_tst (main is the name of the project). So the data is being retrieved from main_tst and not main .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM