reg: How to call multiple APIView in one single web page as a list of items?

153 views
Skip to first unread message

Amitesh Sahay

unread,
Dec 4, 2019, 1:32:25 PM12/4/19
to Django Users
I am in the process of developing a small project using django rest framework, and within that requirement, I need to list every table in one single page. Below is the code snippet.
class QuizList(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'adminView.html'

    def get(self, request):
        queryset = Quiz.objects.all()
        return Response({'quiz': queryset})


class AnswerList(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'adminView.html'

    def get(self, request):
        queryset = Answer.objects.all()
        return Response({'answer': queryset})


class QuestionList(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'adminView.html'

    def get(self, request):
        queryset = Question.objects.all()
        return Response({'question': queryset})

In the above snippet, I am able to list the very first class "QuizView" on my web page. But other than that, when I am trying to add other APIViews, they are simply not happening. Below is the HTML template:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin View</title>
</head>
<body>
<ul>
    {% for quizez in quiz %}

    <a href="" onclick="">{{ quizez.name }}</a>
    {% endfor %}

</ul>

<ul>
    {% for questions in question %}
    <li>{{ questions.label }}</li>
    {% endfor %}
</ul>

<ul>
    {% for answers in answer %}
    <li>{{ answer.text }}</li>
    {% endfor %}
</ul>

</body>
</html>

I tried to put all the for loops within a single "ul", but even that didn't work. Please help.

Thank you

Integr@te System

unread,
Dec 4, 2019, 1:50:02 PM12/4/19
to django...@googlegroups.com
Hi Issuer, 
Plz show result with errors.

Tks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1895937763.9174288.1575484276499%40mail.yahoo.com.

Amitesh Sahay

unread,
Dec 4, 2019, 2:15:46 PM12/4/19
to django...@googlegroups.com
Hi,

There is no error. But based on the urls settings I could see the "QuizList" output to my web page. Other than that, other serializers are not being displayed there. Below is the serializers.py 

from rest_framework import serializers
from .models import Quiz, Question, QuizTakers, Answer, User
from django.contrib.auth import authenticate, login


class userSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = 'email'


class QuizSerializer(serializers.ModelSerializer):
class Meta:
model = Quiz()
fields = ('name', 'questions_count', 'description', 'created', 'roll_out')

def createQuestion(self, validated_data):
return Question().objects.create_question(**validated_data)


class AnswerSerializer(serializers.ModelSerializer):
class Meta:
model = Answer()
fields = ('question', 'text', 'is_correct')


class QuestionSerializer(serializers.ModelSerializer):

class Meta:
model = Question()
fields = ('quiz', 'label', 'order')


class QuizTakerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = QuizTakers()
fields = ('user', 'quiz', 'correct_answers', 'completed', 'timestamp')


Regards,
Amitesh


Integr@te System

unread,
Dec 5, 2019, 11:38:23 AM12/5/19
to django...@googlegroups.com
Hi Issuer,

U see QuizList bc of Question queryset been get validate_data when initilize quiz object instance.

Plz refer to docs to customise ur other view.

Daniel Roseman

unread,
Dec 5, 2019, 5:10:06 PM12/5/19
to Django users
This is not how things work. You don't "add other views" to an existing page. One page equals one view.

I don't know why you are using API views - or DRF - here at all. As the name implies, that's when you're implementing an API, either for external use or to power your page that's built in a front-end framework. Don't use them for standard pages using Django templates: use a standard Django view for that. Again, one view for one page, which includes all the content you need to render that page.
-- 
DR.
Reply all
Reply to author
Forward
0 new messages