I have a question answer quiz. I want each form to be responsible for each query when I publish the forms in html. For example I have it now .
I have to choose the question manually. I want it to happen automatically, i.e. no matter how many questions I have so many forms for each question.
I do not understand what I have to change, view or formset should I use, please if you can help me change the code.
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
import random
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
question=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, "question":question})
I even want each input form to be responsible for the question automatically and not have to manually select the question. Take facebook for example. The comment field for each post belongs to that post huh right? I want to be like that. With the following structure: Question -> Input of this query -> Submit of this input. did you understand? @KenWhitesell
So yes, you are looking to use a ModelFormset, where each instance of the formset is a custom model form. Each instance of that model form should have a hidden field to track the pk of the question along with the text of the question being used as the label for that form.
Also, you’ll probably want to make sure that your formset doesn’t generate any extra forms, and does not allow for new forms to be added or for forms to be removed.
What you want to keep in mind is that the class for your model formset should be Answer, not Question, and that you access the necessary fields from Question through your fk field.
So I would first suggest you try building a model formset for Answer with just the right number of answer forms, to get you comfortable with working with formsets. Then you can add the customization for the specific label on each one.
Thanks a lot I will review these links but if you have any other links that will help me more? In addition to documentation. Because documentation is always a bit difficult in my opinion
are you here? I looked at the model formset and I think it will not work. I do not want to work with 2 forms at once. I want the Answer input to match the Question. with formset I think it works with 2 model forms. @KenWhitesell
No, there are not two forms involved with this. It’s only one form, and that’s for your answer. The questions will be the labels. Each Answer form generated will have a different label, from the corresponding question.