首先,我将两种模型形式融合为一种形式,它们应该显示主题名称和问题类型,但是由于某种原因,出现了三种形式,而第一种形式没有显示选择项,而第二种形式出现了两次,并且我不希望第二个出现在其中,因为我的CSS没有应用在其中。
这是我的model.py
from django.db import models
from home.choices import *
# Create your models here.
class Topic(models.Model):
topic_name = models.IntegerField(
choices = question_topic_name_choices, default = 1)
def __str__(self):
return '%s' % self.topic_name
class Image (models.Model):
image_file = models.ImageField()
def __str__(self):
return '%s' % self.image_file
class Question(models.Model):
questions_type = models. IntegerField(
choices = questions_type_choices, default = 1)
question_topic = models.ForeignKey( 'Topic',
on_delete=models.CASCADE,
blank=True,
null=True)
question_description = models.TextField()
question_answer = models.ForeignKey( 'Answer',
on_delete=models.CASCADE,
blank=True,
null=True)
question_image = models.ForeignKey( 'Image',
on_delete=models.CASCADE,
blank=True,
null=True)
def __str__(self):
return '%s' % self.question_description
class Answer(models.Model):
answer_description = models.TextField()
answer_image = models.ForeignKey( 'Image',
on_delete=models.CASCADE,
blank=True,
null=True)
def __str__(self):
return '%s' % self.answer_description
这是我的forms.py
from django import forms
from betterforms.multiform import MultiModelForm
from .models import Topic, Image, Question, Answer
from .choices import questions_type_choices, question_topic_name_choices
class TopicForm(forms.ModelForm):
topic_name = forms.ModelChoiceField(
queryset = Topic.objects.all(),
widget = forms.Select(
choices=(question_topic_name_choices),
attrs = {'class': 'home-select-one'}
))
class Meta:
model = Topic
fields = ['topic_name',]
class QuestionForm(forms.ModelForm):
question_type = forms.ModelChoiceField(
queryset = Topic.objects.all(),
widget = forms.Select(
choices=(questions_type_choices),
attrs = {'class': 'home-select-two'}
))
class Meta:
model = Question
fields = ['questions_type',]
class QuizMultiForm(MultiModelForm):
form_classes = {
'topics':TopicForm,
'questions':QuestionForm
}
def save(self, commit=True):
objects = super(QuizMultiForm, self).save(commit=False)
if commit:
topic_name = objects['topic_name']
topic_name.save()
question_type = objects['question_type']
question_type.topic_name = topic_name
question_type.save()
return objects
这是我的views.py
from django.shortcuts import render, render_to_response
from django.views.generic import CreateView
from home.models import Topic, Image, Question, Answer
from home.forms import QuizMultiForm
def QuizView(request):
if request.method == "POST":
form = QuizMultiForm(request.POST)
if form.is_valid():
pass
else:
form = QuizMultiForm()
return render(request, "index.html", {'form': form})
这是choices.py
question_topic_name_choices = (
(1, ("Topic #1: Measurements and Uncertainties")),
(2, ("Topic #2: Mechanics")),
(3, ("Topic #3: Thermal Physics")),
(4, ("Topic #4: Waves")),
(5, ("Topic #5: Electricity and Magnetism")),
(6, ("Topic #6: Circular Motion and Gravitation")),
(7, ("Topic #7: Atomic, Nuclear and Particle Physics")),
(8, ("Topic #8: Energy Production")),
(9, ("Topic #9: Wave Phenomena (HL Only)")),
(10, ("Topic #10: Fields (HL Only)")),
(11, ("Topic #11: Electromagnetic Induction (HL Only)")),
(12, ("Topic #12: Quantum and Nuclear Physics (HL Only)")),
(13, ("Option A: Relativity")),
(14, ("Option B: Engineering Physics")),
(15, ("Option C: Imaging")),
(16, ("Option D: Astrophysics"))
)
questions_type_choices = (
(1, ("Multiple Choice Questions")),
(2, ("Problem Solving Questions")))
这是模板
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" id="home-Physics-time-button">It is Physics Time</button>
</form>
{% endblock content %}
谢谢您的帮助!
答案 0 :(得分:0)
您的选择配置不正确。删除右侧的多余()。
questions_type_choices = (
(1, "Multiple Choice Questions"),
(2, "Problem Solving Questions")
)
左存储在数据库中,右以表格显示。