我正在尝试做一些非常简单的事情,但我是Django的新手。我有一个测验系统,用于我正在运行的实验。
models.py中的相关条目如下:
class Flavor(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Passage(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class PassageText(models.Model):
passage = models.ForeignKey(Passage)
flavor = models.ForeignKey(Flavor)
contents = models.TextField()
def __unicode__(self):
return "[%s#%s]" % (self.passage, self.flavor)
class Question(models.Model):
passage = models.ForeignKey(Passage)
text = models.TextField()
def __unicode__(self):
return "[%s#%s]" % (self.passage, self.text)
class AnswerOption(models.Model):
question = models.ForeignKey(Question)
text = models.TextField()
is_correct = models.BooleanField(default=False)
def __unicode__(self):
return "[%s#%s]" % (self.question, self.text)
class TestSubject(models.Model):
GENDER_CHOICES = ( ('M','Male'), ('F','Female'), )
EDUCATION_CHOICES = ( ('SH', 'Some high school'), ('HS', 'High school diploma'), ('SC', 'Some college'), ('CD', 'College degree'), ('MD', 'Master\'s degree'), ('PH','PhD or higher education'), )
GPA_CHOICES = ( ('1', '1.0-1.5'), ('2', '1.5-2.0'), ('3', '2.0-2.5'), ('4', '2.5-3.0'), ('5', '3.0-3.5'), ('6', '3.5-4.0'), ('7', '4.0-4.5'), ('8', '4.5-5.0'), )
ip = models.CharField(max_length=30)
completed = models.BooleanField(default=False)
created_time = models.DateTimeField(default=datetime.now)
time_used = models.IntegerField(default=0, help_text='number of seconds used for test')
age = models.PositiveIntegerField()
gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
education = models.CharField(max_length=2, choices=EDUCATION_CHOICES)
school = models.CharField(max_length=200)
grad_year = models.PositiveIntegerField()
gpa = models.CharField(max_length=2, choices=GPA_CHOICES)
sat_verbal = models.PositiveIntegerField(blank=True)
sat_math = models.PositiveIntegerField(blank=True)
sat_writing = models.PositiveIntegerField(blank=True)
sat_overall = models.PositiveIntegerField(blank=True)
english_is_your_first_language = models.BooleanField()
kerberos_name_if_applying_for_900_credit = models.CharField(max_length=200, blank=True)
def __unicode__(self):
return "[%s#%s]" % (self.ip, self.created_time)
class TestSequence(models.Model):
subject = models.ForeignKey(TestSubject)
order = models.IntegerField(help_text='1..n')
pt = models.ForeignKey(PassageText)
time_used = models.IntegerField(default=0, help_text='number of seconds used for test')
def __unicode__(self):
return "[%s#%d]" % (self.subject, self.order)
class QuestionSequence(models.Model):
tseq = models.ForeignKey(TestSequence)
order = models.IntegerField(help_text='0..n')
question = models.ForeignKey(Question)
selectedanswer = models.ForeignKey(AnswerOption, blank=True, null=True, default=None)
def __unicode__(self):
return "[%s#%d]" % (self.tseq, self.order)
我想基本上有一个页面,它只是给我一个包含所有TestSubject对象(及其所有属性)的表,以及它们所做的段落列表以及它们在该段落中有多少问题的计数。
答案 0 :(得分:0)
好吧,既然没有人帮助过你,请点击此处:
# *snip* -- view:
context = { 'test_subjects' : TestSubject.objects.all() }
return render_to_response('Template', context)
# *snip*
# *snip* -- template:
{% for test_subject in test_subjects %}
{{ test_subject.ip }}
{# ... snip ... #}
{% empty %}
There are no test subjects.
{% endfor %}
其次, 更可取的是将“事物”列表传递给模板并让它发挥作用......除非你有一些特殊的理由不这样做。通过在视图中“转换”和“连接”它们将它们作为字符串传递,会破坏数据和代码之间的分离,并使您的视图“变脏”。
有关此主题的更多信息,请参阅:Views,Templates和Models
另一方面,我认为如果你重新思考模型设计的某些部分会更好,我看到那里有一些冗余。存储测试对象的名称将是一个良好且用户友好的想法。最后,flavor
不应该flavour
?即使您没有在代码中更改它,您也可能希望将其用于英国英语使用者。