DesiredDesigns
DesiredDesigns

Reputation: 103

Object Not Iterable

I am having an odd problem... This code was working fine until I worked on some of the other views, and now, it isn't working.

Here is my view:

def showTickets(request, project_slug):
    project = Project.objects.get(slug=project_slug)
    tickets = Ticket.objects.get(project=project)
    payload = { 'project':project, 'tickets':tickets}
    return render(request, 'project/tickets.html', payload)

Template:

{% extends 'project/base.html' %}

{% block title %}Tickets: {{project.name}}{% endblock %}

{% block main %}

<div id="project-nav">
    <span><a href="/project/{{project.slug}}/">Tickets</a></span>
    <span><a href="/book/{{book.slug}}{{book.name}}">Docs</a></span>
    <span><a href="/project/{{project.slug}}/browse">Browser</a></span>
</div>
<div id="action-nav">
    {% block actions %}
    <span><a href="/project/{{project.slug}}/tickets/create">Create Ticket</a></span>
    <span><a href="/project/{{ project.slug }}/tickets/recent/">Recent Activity</a></span>
    <span><a href="/project/{{ project.slug }}/tickets/my/">My Tickets</a></span>
    {% endblock %}
</div>
{% for ticket in tickets %}


<div class="ticket">
    <div class="ticket-header">
        <div class="ticket-title">
            <a href="/project/ticket/{{ticket.pk}}">{{ticket.subject}}</a>
        </div>
        <div id="ticket-number">
            #{{ticket.pk}}
        </div>
        <div id="ticket-state">
            {{ticket.get_state_display}}
        </div>
        <div id="ticket-info">
            Reported by {{ticket.created_by}} | created: {{ticket.created_on }} | modified: {{ticket.modified_on}}
        </div>
    </div>
</div>
{% endfor %}
</div>

{% endblock %}

Error:

Template error:
In template c:........\project\tickets.html, error at line 19
   Caught TypeError while rendering: 'Ticket' object is not iterable

This was working fine until I worked on some other views...not sure why it isn't working now? If any one can help I'd appreciate it!

Upvotes: 2

Views: 7348

Answers (2)

J. Landgrave
J. Landgrave

Reputation: 96

project = Project.objects.get(slug=project_slug) 
tickets = Ticket.objects.get(project=project) 

These two lines are the cause of your troubles. They aren't returning an iterable, such as a list, but an object, which isn't iterable at all. Instead of a get, use a filter instead, which will return a list.

Upvotes: 7

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799062

The get() method returns a single object. Perhaps you meant to use the filter() method instead?

Upvotes: 3

Related Questions