首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django Football装备

Django Football装备
EN

Stack Overflow用户
提问于 2016-03-28 07:51:21
回答 1查看 334关注 0票数 1

我正在用Django创建一个足球网站,遇到了一个问题。目前,我的主页和fixture页面在不同的应用程序中。我让fixture页面正常工作,这样它就可以显示由管理页面添加的fixture。我想在主页上包括下一个即将到来的固定装置,但我有一些导入数据的问题。

目前,我的fixtures/models.py文件如下所示

代码语言:javascript
复制
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看起来像这样

代码语言:javascript
复制
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看起来像这样:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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中);

代码语言:javascript
复制
  {% for fixture in matches %}
      <div>
          <p>Vs {{ fixture.firstfixture.opponents }} - {{ fixture.firstfixture.match_date }}</p>
      </div>
  {% endfor %}

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2016-03-28 07:53:14

您必须将all作为函数调用;否则它只是一个可调用的。

代码语言:javascript
复制
matches = First.objects.all()

而不是

代码语言:javascript
复制
matches = First.objects.all

编辑:您必须实际访问您的第一个实例的FK才能获得opponents

代码语言:javascript
复制
{% for fixture in matches %}
    <div>
        <p>Vs {{ fixture.firstfixture.opponents }} - {{ fixture.firstfixture.match_date }}</p>
    </div>
{% endfor %}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36254042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档