我正在创建一个包含以下模型/视图的简单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,但所有回馈的都是 退出: 没有任何退回。谁能让我知道出了什么问题?提前谢谢。
答案 0 :(得分:3)
在模板中将room.room_exit.all
更改为room.exits.all