我有一个
class Questions(models.Model):
question = models.CharField(max_length=150)
created_by = models.CharField(max_length=30)
def __unicode__(self):
return self.question
现在我需要这样的输出(所以我可以像 djangos 一样使用它{{ form.as_p }}
):
<div id="question-1">The string in question"</div>
<div id="question-2">The string in another question"</div>
...
随着loq = Questions.objects.filter(created_by=user)
我[<Questions: My first question!>,...]
进去str(loq)
。
有比使用搜索更简单的方法str(loq)
吗.find()
?
编辑:
以这种方式解决它(感谢Samuele Mattiuzzo):
模型.py:
class Questions(models.Model):
question = models.CharField(max_length=150)
created_by = models.ForeignKey(User)
def __unicode__(self):
return self.question
视图.py:
def ViewQuestions(request):
if request.user.is_authenticated():
loq = Questions.objects.filter(created_by=request.user)
return render(request, "main/questions.html", {'loq': loq})
else:
return HttpResponseRedirect("/")
问题.html:
{% for q in loq %}
<div id="question-{{ forloop.counter }}">{{ q.question }}</div>
{% endfor %}