Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

python - django's form is not saved in store

I have a quiz form and when I want to save the answer the model does not store its question. I want to validate the form and save it when if form.is_valid (): I remove it and instead of AnswerForm I use the direct Answer model then it saves but not so. How to change it so that I can validate it and save it with the help of the form?

my code ->

models.py

from django.db import models

# Create your models here.
from django.core.exceptions import ValidationError

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="questions")
    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,Answer
import random
from django.forms import modelformset_factory



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):
    questions=Question.objects.all()
    form=AnswerForm()
    if request.method=="POST":
        if form.is_valid():
            qu=request.POST["i_id"]
            question_id=int(qu)
            answ=AnswerForm(questin=question_id,answer=request.POST["answer"])
            answ.save()
            
            
            
    return render(request, "question/ans.html", {"form":form, "questions":questions})

ans.html

<!DOCTYPE html>
<html>
    <head>
        <title>question</title>
    </head>

    <body>
        
        {% for i in questions %}
        <form method="POST" required>
            
            {% csrf_token %}
            
            
                
                {{i}}
                <input type="hidden" name="i_id" value="{{ i.id }}"/>
                {% for a in form.answer %}
                    {{a}}
                    
                {% endfor %}
                <input type="submit" name="sub">
           
        </form>
        
        {% endfor %}
        
        
        

    </body>
</html>
question from:https://stackoverflow.com/questions/65904984/djangos-form-is-not-saved-in-store

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...