2016-03-27 23 views
1

我在Django創建了一個足球網站,並且遇到了問題。目前我的主頁和夾具頁面在不同的應用程序中。我有夾具頁面工作,所以它顯示管理頁面添加的夾具。我想在主頁上包含下一個即將到來的燈具,但我在導入數據時遇到了一些問題。Django Football 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 

和我的燈具/ 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 
    }) 

我家/ 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') 

and 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 fixtures app工作的for循環是(在HTML中);

{% for fixture in matches %} 
     <div> 
      <p>Vs {{ fixture.firstfixture.opponents }} - {{ fixture.firstfixture.match_date }}</p> 
     </div> 
    {% endfor %} 

在此先感謝

回答

1

必須調用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 %} 
+0

參見編輯; 'First'實例沒有'對手'或'match_date'屬性;您必須如上所示訪問相關的'Fixture'實例。 –

+0

對不起,仍然沒有變化 –

+0

另外,一定不要在模板中調用'matches.all';相反,只需使用'matches'。您正在訪問First實例的Queryset。 –

相關問題