2016-03-27 7 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マイホーム/ models.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 
    }) 

のように見えるようになっています

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ループは、フィクスチャアプリケーションのために働く(HTMLで);事前に

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

おかげ

答えて

1

あなたは関数としてallを呼び出す必要があります。それ以外の場合は呼び出し可能です。代わりに

matches = First.objects.all 

EDIT

matches = First.objects.all() 

:あなたが実際にopponentsを得るためにあなたの最初のインスタンスのFKにアクセスする必要があります。

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

編集を参照してください。 'First'インスタンスには' opponents'や 'match_date'属性はありません。上記のように関連する 'Fixture'インスタンスにアクセスする必要があります。 –

+0

申し訳ありませんが、まだ変更はありません –

+0

また、テンプレートで 'matches.all'を呼び出さないようにしてください。代わりに、単に 'matches'を使用してください。 First Queryset of Firstインスタンスにアクセスしています。 –

関連する問題