我正在用Django创建一个足球网站,遇到了一个问题。目前,我的主页和fixture页面在不同的应用程序中。我让fixture页面正常工作,这样它就可以显示由管理页面添加的fixture。我想在主页上包括下一个即将到来的固定装置,但我有一些导入数据的问题。
目前,我的fixtures/models.py文件如下所示
from django.db import models
from django.utils import timezone
class Fixture(models.Model):
author = models.ForeignKey('auth.User')
opponents = models.CharField(max_length=200)
match_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.match_date = timezone.now()
self.save()
def __str__(self):
return self.opponents我的fixtures/views.py看起来像这样
from django.shortcuts import render_to_response
from django.utils import timezone
from fixtures.models import Fixture
def games(request):
matches = Fixture.objects.filter(match_date__gte=timezone.now()).order_by('match_date')
return render_to_response('fixtures/games.html', {'matches':matches
})我的home/models.py看起来像这样:
from django.utils import timezone
from django.db import models
from fixtures.models import Fixture
class First(models.Model):
firstfixture = models.ForeignKey('fixtures.Fixture')和home/views.py:
from django.utils import timezone
from home.models import First
def index(request):
matches = First.objects.all()
return render_to_response('home/index.html', {'matches':matches
})我已经尝试了许多for for循环的组合,但都没有显示所需的信息。我的for循环适用于fixtures应用程序是(在HTML中);
{% for fixture in matches %}
<div>
<p>Vs {{ fixture.firstfixture.opponents }} - {{ fixture.firstfixture.match_date }}</p>
</div>
{% endfor %}提前感谢
发布于 2016-03-28 07:53:14
您必须将all作为函数调用;否则它只是一个可调用的。
matches = First.objects.all()而不是
matches = First.objects.all编辑:您必须实际访问您的第一个实例的FK才能获得opponents。
{% for fixture in matches %}
<div>
<p>Vs {{ fixture.firstfixture.opponents }} - {{ fixture.firstfixture.match_date }}</p>
</div>
{% endfor %}https://stackoverflow.com/questions/36254042
复制相似问题