On Wednesday, 4 December 2019 18:32:25 UTC, Amitesh Sahay wrote:
-- 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
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.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/adacdb3e-0fb0-43ab-9cd8-733b8b7adb77%40googlegroups.com.
No comments:
Post a Comment