title: How to save a form in a database (django)
body: <p>I have a quiz. I have 2 pages, I create the questions and their answers on the first page and I put these questions on the second page, but I want these questions to have the Answer model input, i.e. each question has its own input. When I try to set a query with an Id in the view and the unsuitable Answer form to save does not work, it is not stored in the database. How do I save?</p>
  <p>models.py</p>
  <pre><code>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, related_name=&quot;questions&quot;)
      answer=models.CharField(max_length=100,blank=True)
  
      def __str__(self):
          return str(self.questin)
  </code></pre>
  <p>forms.py</p>
  <pre><code>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=&quot;__all__&quot;
  
  
  class AnswerForm(forms.ModelForm):
      class Meta:
          model=Answer
          fields=&quot;__all__&quot;
  
  
  </code></pre>
  <p>views.py</p>
  <pre><code>
  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, &quot;question/base.html&quot;, {&quot;form&quot;:form})
  
  def ans(request):
      form=AnswerForm
      questions=Question.objects.all()
      if request.method==&quot;POST&quot;:
          instance=Question.objects.get(id=request.POST['i_id'])
          print(instance)
          form=AnswerForm(request.POST, instance=instance)
          if form.is_valid():
              form.save()
              
      return render(request, &quot;question/ans.html&quot;, {&quot;form&quot;:form, &quot;questions&quot;:questions})
      
  </code></pre>
  <p>ans.html</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;question&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          {% for i in questions %}
          
          &lt;form method=&quot;POST&quot; novalidate&gt;
              {% csrf_token %}
              &lt;input type=&quot;hidden&quot; name=&quot;i_id&quot; value=&quot;{{ i.id }}&quot; /&gt;
                  {{i}}
                 {% for a in form %}
  
                 {{a}}
                 
  
                 {% endfor %}
              
              &lt;input type=&quot;submit&quot; name=&quot;sub&quot;&gt;
          &lt;/form&gt;
          {% endfor %}
          
          
  
      &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>