大家好,我有一些我不知道如何使其发挥作用的东西。 我正在制作一份调查问卷,例如,页面上会显示问题,还有一个收音机,如用户可以选择的答案形式列表。
我试一试,但我不知道如何将问题链接到我的表格,你能帮忙吗?
models.py:
from django.db import models
# Create your models here.
class Questionnaire(models.Model):
question = models.CharField(max_length=600)
multiple_answer = models.BooleanField(default=False)
def __str__(self):
return self.question
class Answers(models.Model):
question_id = models.ForeignKey('Questionnaire', on_delete=models.CASCADE)
answer = models.CharField(max_length = 600)
def __str__(self):
return self.answer
class Test(models.Model):
user = models.ForeignKey('registration.MyUser')
created_at = models.TimeField(auto_now_add=True)
closed = models.BooleanField(default=False)
class UserAnswer(models.Model):
test_id = models.ForeignKey('Test')
question = models.ForeignKey('Questionnaire')
answer = models.ForeignKey('Answers')
views.py:
def AnswerChoiceSelect(request):
#import pdb; pdb.set_trace()
if request.method == "POST":
answer_form = QuestionChoices(request.POST)
if answer_form.is_valid():
print("working")
else:
print('Non Valid form')
else:
answer_form = QuestionChoices(request.user)
return render(request,'question_template.html',
{'answer_form':answer_form })
forms.py:
from django import forms
from django.contrib.auth.models import User
from registration.models import MyUser
from .models import Questionnaire, Answers, Test, UserAnswer
from django.contrib.auth import get_user_model
User = get_user_model()
class QuestionChoices(forms.Form):
question_choice = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=None)
def __init__(self,questionnaire, *args, **kwargs):
super(QuestionChoices, self).__init__(*args, **kwargs)
current_question = Questionnaire.objects.get(id = question_id)
self.fields['question_choice'].queryset = Answers.objects.filter(question_id = Current_question.id)