tuna
tuna

Reputation: 6351

Can not see the result of query in template

I want to see my query results in an html after a ajax get request. Please find my code below, def tanim_ajax(request):

#views.py
def tanim_ajax(request):

    if request.GET.get('q'):

        q = request.GET.get('q')
        results = Title.objects.filter(title__contains=q),
        print results
        return render_to_response('tanim/ajax.html',
                                    {'results':results},
                                    context_instance=RequestContext(request))

#models.py
class Title(models.Model):
    title = models.CharField(max_length=100,verbose_name="Başlık")
    category = models.ForeignKey(Category,verbose_name="Kategori")
    active = models.BooleanField(default=False)
    slug = models.SlugField(editable=False)

    def __unicode__(self):
        return self.title

Everything works fine for template

{% for i in results %}
<br>{{i}}
{% endfor %}

I can see the result as [<Title: foo>] when i checked thehttp://127.0.0.1:8000/tanimlar/tanim_ajax/?q=foourl.

But when I have the following template

 {% for i in results %}
<br>{{i.title}}
{% endfor %}

I dont see any results. Any idea ?

Upvotes: 0

Views: 74

Answers (1)

Michael Place
Michael Place

Reputation: 3046

Because you're passing a QuerySet into the context. Instead, try appending values() to your query which will return a dictionary.

results = Title.objects.filter(title__contains=q).values()

Upvotes: 1

Related Questions