I am making a simple web app that consists of the following models/views:
# Models
from django.db import models
class Area(models.Model):
name = models.CharField(max_length=40)
def __unicode__(self):
return self.name
class Room_Exit(models.Model):
direction = models.CharField(max_length=20)
secret = models.BooleanField()
def __unicode__(self):
return self.direction
class Room(models.Model):
name = models.CharField(max_length=40)
description = models.TextField()
environment = models.CharField(max_length=40)
secret = models.BooleanField()
outdoors = models.BooleanField()
area = models.ForeignKey(Area)
exits = models.ManyToManyField(Room_Exit)
def __unicode__(self):
return self.name
# Views
from django.shortcuts import render_to_response, get_object_or_404
from wb.models import Area, Room_Exit, Room
def arealist(request):
areas = Area.objects.all().order_by('name')
return render_to_response('areas/arealist.html', {'areas': areas})
def roomlist(request, area_id):
a = get_object_or_404(Area, pk=area_id)
return render_to_response('areas/roomlist.html', {'area': a})
def roomdetail(request, area_id, room_id):
a = get_object_or_404(Area, pk=area_id)
r = get_object_or_404(Room, pk=room_id)
return render_to_response('areas/roomdetail.html', {'room': r})
I am trying to make a template that will print out the properties of a Room. I am able to print out every property except when it comes to the ManyToManyField. There is a room created in the database that has 2 Room_Exits. Here is the template:
<h1>{{ room.name }}</h1>
<ul>
<li>
ID: {{ room.id }}
</li>
<li>
Description: {{ room.description }}
</li>
<li>
Environment: {{ room.environment }}
</li>
<li>
Secret room: {{ room.secret }}
</li>
<li>
Outdoor room: {{ room.outdoors }}
</li>
<li>
Area: {{ room.area }}
</li>
<li>
Exits:
{% for exits in room.room_exit.all %}
{{ exits }}
{% endfor %}
</li>
</ul>
I am trying to loop through the Room_Exits of the Room, but all it gives back is
Exits:
without anything being returned. Can anyone give me an idea as to what’s going wrong? Thanks in advance.
change
room.room_exit.alltoroom.exits.allin your template