首页 > 解决方案 > how to render a HTML template through django REST APIView?

问题描述

I have two HTML page,

1) adminView.html = list all the model serializers for which I have built the views. It has Quiz listed from the models. 

2) quiz.html = It has quiz based form that I want to render when I click the Quiz view in the adminView.html.

I have followed this official django REST doc to write my POST function to create a "Quiz"

But I do not see any effect taking on my web page. Below are the HTML templates:

adminView.html

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

    <a href="" onclick="">{{ quizez.name }}</a>
    {% endfor %}
<br>
</ul>
<br>
<ul>
    {% for questions in question %}
    <a href="" onclick="">{{ questions.label }}</a>
    {% endfor %}
</ul>

<ul>
    {% for answers in answer %}
    <a href="" onclick="">{{ answers.text }}</a>
    {% endfor %}
</ul>

</body>
</html>

quiz.html

{% load rest_framework %}

<h1>Profile - {{ profile.name }}</h1>

<div class="container">
<form action="{% url 'quiz' pk=profile.pk %}" form id="MCQ" method="post">
  {% csrf_token %}
  {% render_form serializer %}
  {{ form }}

<ul class="quiz">
  <li>
    <h4>How many letters are there in "JS"?</h4>
    <ul class="choices">
      <li><label><input type="radio" name="question0" value="A"><span>2</span></label></li>
      <li><label><input type="radio" name="question0" value="B"><span>1</span></label></li>
      <li><label><input type="radio" name="question0" value="C"><span>3</span></label></li>
      <li><label><input type="radio" name="question0" value="D"><span>4</span></label></li>
    </ul>
  </li>
  <li>
    <h4>How many letters are there in "BMX"?</h4>
    <ul class="choices">
      <li><label><input type="radio" name="question1" value="A"><span>2</span></label></li>
      <li><label><input type="radio" name="question1" value="B"><span>1</span></label></li>
      <li><label><input type="radio" name="question1" value="C"><span>3</span></label></li>
      <li><label><input type="radio" name="question1" value="D"><span>4</span></label></li>
    </ul>
  </li>
  <li>
    <h4>How many letters are there in "A"?</h4>
    <ul class="choices">
      <li><label><input type="radio" name="question2" value="A"><span>2</span></label></li>
      <li><label><input type="radio" name="question2" value="B"><span>1</span></label></li>
      <li><label><input type="radio" name="question2" value="C"><span>3</span></label></li>
      <li><label><input type="radio" name="question2" value="D"><span>4</span></label></li>
    </ul>
  </li>
</ul>

<button class="view-results" onclick="returnScore()">View Results</button>
<span id="myresults" class="my-results">My results will appear here</span>
</form>
    </div>

views.py

from django.shortcuts import render, redirect
from rest_framework.generics import *
from .models import *
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import *
from rest_framework import status


class AllModelListView(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'adminView.html'
    serializer_class = QuizSerializer

    def get(self, request):
        quiz_queryset = Quiz.objects.all()
        answer_queryset = Answer.objects.all()
        question_queryset = Question.objects.all()
        # return Response({'quiz': queryset})
        return Response({
            'quiz': quiz_queryset,
            'answer': answer_queryset,
            'question': question_queryset
        })

    def post(self, request, pk):
        profile = get_object_or_404(Quiz, pk=pk)
        serializer_class = QuizSerializer(profile, data=request.data)

        if not serializer_class.is_valid():
            return Response({'serializer': serializer_class, 'profile': profile})
        serializer_class.save()
        return redirect('adminView')

I assume that I am missing something that is not letting the feature enabled on the web page.

I hope I was able to explain my issue statement properly. However, please ask me questions if anything unclear, I will try to explain in another way.

this link is the Gdrive link if somebody wants to try it out.

Thank you,

标签: htmldjangodjango-rest-frameworkpython-3.6

解决方案


推荐阅读