I have a
class Questions(models.Model):
question = models.CharField(max_length=150)
created_by = models.CharField(max_length=30)
def __unicode__(self):
return self.question
Now i need a output like this (so i can use it like djangos {{ form.as_p }}):
<div id="question-1">The string in question"</div>
<div id="question-2">The string in another question"</div>
...
With loq = Questions.objects.filter(created_by=user) i get [<Questions: My first question!>,...] in str(loq).
Is there an easier way than to search str(loq) with .find()?
EDIT:
Solved it this way (thanks to 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
views.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("/")
questions.html:
{% for q in loq %}
<div id="question-{{ forloop.counter }}">{{ q.question }}</div>
{% endfor %}
create a template file (say questions.html) like this:
in your views.py, you have to return your queryset (which is your loq)
locals() is the dictionary containing your loq. here i’m assuming you already know how django works. i don’t really get what you mean with that “.find()” statement, tho.
you may also want to change the created_by field from CharField to ForeignKey poiting to the User class
check the docs about querysets and views for more knowledge on the subject.
if this doesn’t answer, provide some more explanation and i’ll be updating the answer