0

私は

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に感謝):

models.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("/")

question.html:

{% for q in loq %}
<div id="question-{{ forloop.counter }}">{{ q.question }}</div>
{% endfor %}
4

1 に答える 1