简体   繁体   English

如何将表单保存在数据库中(django)

[英]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, ie each question has its own input.我有 2 页,我在第一页创建问题及其答案,并将这些问题放在第二页,但我希望这些问题有 Answer model 输入,即每个问题都有自己的输入。 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.当我尝试在视图中设置带有 Id 的查询并且不适合保存的答案表单不起作用时,它不会存储在数据库中。 How do I save?我该如何保存?

models.py模型.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 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视图.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 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>

Try to different approaches to create the pages in html and c尝试不同的方法在 html 和 c 中创建页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM