我正在尝试创建一个调查应用。但是,当我创建2个调查时,第二个问题中的问题不会出现在模板中。调查1(效果很好)有3个问题,调查2有2个问题(模板上没有显示)。
以下是我的模特:
class Survey(models.Model):
name = models.CharField(max_length=400)
description = models.TextField()
user = models.ForeignKey(User, related_name="user_survey_set")
def __unicode__ (self):
return (self.name)
def questions(self):
if self.pk:
return Question.objects.filter(survey=self.pk)
else:
return None
def user_data(self):
p = self.user
return p.name
class Category(models.Model):
name = models.CharField(max_length=400)
survey = models.ForeignKey(Survey)
def __unicode__(self):
return (self.name)
def validate_list(value):
values = value.split('')
if len(values) < 2:
raise ValidationError("A resposta precisa ter mais de um item")
pass
class Question(models.Model):
TEXT = 'text'
RADIO = 'radio'
SELECT = 'select'
SELECT_MULTIPLE = 'select-multiple'
INTEGER = 'integer'
QUESTION_TYPES = (
(TEXT, 'text'),
(RADIO, 'radio'),
(SELECT, 'select'),
(SELECT_MULTIPLE, 'Select Multiple'),
(INTEGER, 'integer'),
)
text = models.TextField()
required = models.BooleanField()
category = models.ForeignKey(Category, blank=True, null=True)
survey = models.ForeignKey(Survey)
question_type = models.CharField(max_length=200, choices=QUESTION_TYPES, default=TEXT)
choices = models.TextField(blank=True, null=True,
help_text='if the question type is "radio", "select" or "select multiple" provide a comma separated list for this question')
这是观点:
def SurveyDetail(request, id):
survey = Survey.objects.get(id=id)
category_items = Category.objects.filter(survey=survey)
categories = [c.name for c in category_items]
print 'categories for this survey:'
print categories
if request.method == 'POST':
form = ResponseForm(request.POST, survey=survey)
if form.is_valid():
response = form.save()
return HttpResponseRedirect("/confirm/%s" % response.interview_uuid)
else:
form = ResponseForm(survey=survey)
print form
# TODO sort by category
return render(request, 'survey.html', {'response_form': form, 'survey': survey, 'categories': categories})
代码有什么问题?
答案 0 :(得分:0)
已经遇到了问题。
新调查需要一个新类别来显示模板中的问题。