html - 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,
解决方案
推荐阅读
- angularjs - ng-repeat 在 ui-popover-html 中不起作用
- ruby-on-rails - 使用 accept_nested_attributes_for 和 fields_for 创建控制器方法
- c++ - C++ WinHTTP 库生成符号错误
- excel - 不同时间段的值相加并匹配特定日期
- .net-core - Reading from app.config in .NET Core application referencing .NET Standard library
- android - 如何调试 Android Native 崩溃日志(可能是多个 webview 问题)?
- nativescript - ios崩溃中的Nativescript自动完成
- javascript - 当您按下模态框上的取消时,Magnific Pop up 不会关闭
- c++11 - Repeating values for in a random bytes generator in c++
- c# - Xamarin.Forms - ListView 显示在 UWP 但不是 Android