我正在制作一个简单的 Web 应用程序,其中包含以下模型/视图:
# 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})
我正在尝试制作一个可以打印出房间属性的模板。除了ManyToManyField 之外,我能够打印出所有属性。在数据库中创建了一个具有 2 个 Room_Exits 的房间。这是模板:
<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>
我正在尝试遍历房间的 Room_Exits,但它返回的只是 Exits:没有返回任何东西。谁能给我一个关于出了什么问题的想法?提前致谢。