I have a question and answer quiz and when I put it in HTML I have such a result image To save a question, I must first manually select the question. So I want this to happen automatically. Each question will appear automatically. But I could not understand the view I have to change if html and what I have to change
this is my code -->
models.py
from django.db import models
# Create your models here.
class Question(models.Model):
question=models.CharField(max_length=100)
answer_question=models.CharField(max_length=100, default=None)
def __str__(self):
return self.question
class Answer(models.Model):
questin=models.ForeignKey(Question, on_delete=models.CASCADE)
answer=models.CharField(max_length=100,blank=True)
def __str__(self):
return str(self.questin)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.forms import ModelForm
from .models import Question,Answer
class QuestionForm(forms.ModelForm):
class Meta:
model=Question
fields="__all__"
class AnswerForm(forms.ModelForm):
class Meta:
model=Answer
fields="__all__"
views.py
from django.shortcuts import render
from django.shortcuts import render, HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from .forms import QuestionForm,AnswerForm
from .models import Question
def home(request):
form=QuestionForm
if request.method=='POST':
form=QuestionForm(request.POST)
if form.is_valid():
form.save()
return render(request, "question/base.html", {"form":form})
def ans(request):
form=AnswerForm
e=Question.objects.all()
if request.method=="POST":
form=AnswerForm(request.POST)
if form.is_valid():
form.save()
return render(request, "question/ans.html", {"form":form, "e":e})
base.html
<!DOCTYPE html>
<html>
<head>
<title>question</title>
</head>
<body>
{% for i in e %}
<form method="POST" novalidate>
{% csrf_token %}
{% for a in form %}
{{a}}
{% endfor %}
<input type="submit" name="sub">
</form>
{% endfor %}
</body>
</html>