【问题标题】:How to save a form in a database (django)如何将表单保存在数据库中(django)
【发布时间】:2021-04-26 00:06:37
【问题描述】:

我有一个测验。我有 2 页,我在第一页创建问题及其答案,并将这些问题放在第二页,但我希望这些问题具有 Answer 模型输入,即每个问题都有自己的输入。当我尝试在视图中设置带有 Id 的查询并且不适合保存的答案表单不起作用时,它不会存储在数据库中。如何保存?

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>

【问题讨论】:

    标签: python python-3.x django forms django-models


    【解决方案1】:

    尝试不同的方法在 html 和 c 中创建页面

    【讨论】:

    • 你能具体解释一下吗?
    • 请尝试用他正在使用的堆栈(Django)解决 OP 的问题并保持话题。
    • @twrought 到底是什么问题?
    • 这没有回答问题。应该是评论。
    • 如果有人知道问题的答案,请回答他
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2016-10-15
    • 2020-09-15
    • 2017-03-06
    • 2018-03-16
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多