我正在进行一个django项目,我必须在其中创建试卷并向其中添加问题。
我需要创建视图的帮助,在该视图中,我首先选择一个主题和章节,然后提交,然后将与它们相关的所有问题作为复选框,以选择多个值并为每个问题输入分数。
我已经使用SimpleIsBetterThanComplex创建了一个依赖下拉列表,但这使我一次只添加一个我不喜欢的问题。
请任何人提供解决方案或关于如何实现此目的的提示。提前谢谢。
models.py
from questions.models import Question, Subject, Chapter
class SubjectiveName(models.Model):
name = models.CharField(max_length=200)
date = models.DateField(default=date.today)
for_class = models.PositiveSmallIntegerField(
validators=(
MinValueValidator(1),
MaxValueValidator(12),
)
)
PART = (
("A", "A"),
("B", "B"),
("C", "C"),
("D", "D"),
("E", "E"),
)
part = models.CharField(max_length=1, choices=PART, null=True,
blank=True)
max_marks = models.PositiveSmallIntegerField()
def marks_added(self):
sum = 0
for question in self.subjective.all():
sum += question.marks
return sum
def __str__(self):
return self.name
def get_absolute_url(self):
return urls.reverse("subjective:detail", args=(str(self.pk)))
def get_chapters(self):
chapters = {}
for question in self.subjective.all():
chapter = question.questions.chapter
chapters[chapter] = chapters.get(chapter, 0) + 1
return chapters
class SubjectiveQuestions(models.Model):
exam = models.ForeignKey(SubjectiveName, on_delete=models.CASCADE,
related_name="subjective")
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE)
questions = models.ForeignKey(Question, on_delete=models.SET_NULL,
null=True)
marks = models.PositiveSmallIntegerField(blank=True, null=True,
help_text="If adding a question- leave blank to assign the default
marks")
def __str__(self):
return self.questions.question
def get_absolute_url(self):
return urls.reverse("subjective:detail", args=(str(self.exam_id)))
views.py
from .forms import SubjectiveQuestionForm
from questions.models import Chapter, Question
class SubjectiveQuestionsCreateView(gn.edit.CreateView):
model = md.SubjectiveQuestions
form_class = SubjectiveQuestionForm
template_name = "subjective/new_question.html"
def form_valid(self, form):
form.instance.exam_id = self.kwargs.get("pk")
if not form.instance.marks:
form.instance.marks = form.instance.questions.marks
return super(SubjectiveQuestionsCreateView,self).form_valid(form)
处理ajax请求。
def load_questions(request):
subject_id = request.GET.get("subject")
chapter_id = request.GET.get("chapter")
if subject_id and not chapter_id:
questions = Question.objects.filter(subject_id=subject_id)
return render(request, "subjective/questions_dropdown_list.html", {
"questions": questions
})
elif chapter_id:
questions = Question.objects.filter(subject_id=subject_id, chapter_id=chapter_id)
return render(request, "subjective/questions_dropdown_list.html", {"questions": questions}
def load_chapters(request):
subject_id = request.GET.get("subject")
question_id = request.GET.get("question")
if not question_id:
chapters = Chapter.objects.filter(subject_id=subject_id)
return render(request, "subjective/chapters_dropdown_list.html", {"chapters": chapters})
elif question_id:
chapter = Question.objects.get(pk=question_id).chapter
chapters = Chapter.objects.exclude(name=chapter).filter(subject_id=subject_id)
return render(request, "subjective/chapters_dropdown_list.html", {"chapter": chapter, "chapters": chapters})
forms.py
from .models import SubjectiveQuestions
from questions.models import Chapter, Question
class SubjectiveQuestionForm(forms.ModelForm):
class Meta:
model = SubjectiveQuestions
fields = "subject", "chapter", "questions", "marks"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["chapter"].queryset = Chapter.objects.none()
self.fields["questions"].queryset = Question.objects.none()
if "subject" in self.data:
try:
subject_id = int(self.data.get("subject"))
chapter_id = int(self.data.get("chapter"))
self.fields["chapter"].queryset = Chapter.objects.filter(subject_id=subject_id)
self.fields["questions"].queryset = Question.objects.filter(subject_id=subject_id, chapter_id=chapter_id)
except (ValueError, TypeError):
pass