Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - How to save a form in a database (django)

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?

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, 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
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
    questions=Question.objects.all()
    if request.method=="POST":
        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, "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" novalidate>
            {% csrf_token %}
            <input type="hidden" name="i_id" value="{{ i.id }}" />
                {{i}}
               {% for a in form %}

               {{a}}
               

               {% endfor %}
            
            <input type="submit" name="sub">
        </form>
        {% endfor %}
        
        

    </body>
</html>

question from:https://stackoverflow.com/questions/65829645/how-to-save-a-form-in-a-database-django

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

1 Reply

0 votes
by (71.8m points)

Try to different approaches to create the pages in html and c


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...