user2071812
user2071812

Reputation: 47

Django TypeError - object is not iterable

I'm trying to get started with Django and am getting the following TypeError when attempting to access a url I defined: 'object is not iterable'

Here's by code:

models.py

from django.db import models
from django.contrib.auth.models import User
from pubs.models import Pub

# Create your models here.
class Event (models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=500)    
    admin = models.ForeignKey(User)
    def __str__(self):
        return self.name

class Event_Instance (models.Model):
    Event = models.ForeignKey(Event)
    location = models.ForeignKey(Location)
    additional_description = models.CharField(max_length=200)
    display_desc = models.BooleanField(default='true')
    time = models.DateTimeField('Event Date & Time')
    def __str__(self):
        return self.Event.name

urls.py

from django.conf.urls import patterns, url
from events import views

urlpatterns = patterns('',
                       url(r'^(?P<event_id>\d+)/instance/(?P<event_instance_id>\d+)/$',views.event_detail, name='event_detail'),
                       )

views.py

from django.shortcuts import render
from events.models import Event, Event_Instance
from django.http import HttpResponse, Http404


# Create your views here.
def event_detail (request, event_id, event_instance_id):
    try:
        event_instance = Event_Instance.objects.get(pk = event_instance_id)
    except Event_Instance.DoesNotExist:
        raise Http404
    return render(request, 'events/event_instance.html', event_instance)

When I run the Event_Instance.objects.get(pk = 1) it brings up what I want, so it must be something to do with the argument I'm passing in, or the fact that I'm passing more than one argument in?

Am I missing something really simple?

Thanks

Upvotes: 0

Views: 4082

Answers (1)

knbk
knbk

Reputation: 53729

render takes a dictionary of keywords that should be passed to the view. So in your example, you should use:

return render(request, 'events/event_instance.html', {'event_instance': event_instance})

Then in your template you can use:

{{ event_instance }}
{{ event_instance.location }}
etc.

Upvotes: 4

Related Questions