2016-03-27 15 views
1

Django'da bir futbol sitesi yaşıyorum ve bir sorun yaşıyorum. Şu anda ana sayfam ve fikstür sayfam farklı uygulamalarda. Fikstür sayfasının çalışmasını sağladım, böylece fikstürleri yönetici sayfasının eklediği gibi görüntüler. Bir sonraki gelecek fikstürü ana sayfaya eklemek istiyorum, ancak verileri içe aktarmada bazı problemler yaşıyorum.Django Futbol Maçları

anda benim fikstürler/models.py dosyası bu

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 

benziyor ve

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 
    }) 

Evim/models.py benziyor gibi benim fikstürler/views.py görünür:

from django.utils import timezone 
from django.db import models 

from fixtures.models import Fixture 

class First(models.Model): 
    firstfixture = models.ForeignKey('fixtures.Fixture') 

ve ev/gösterim.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 
    }) 

Döngü için birçok kombinasyon denedim ama hiçbir şey gerekli bilgiyi göstermiyor. Fikstür uygulaması için çalışan for döngüsüm (HTML); peşin

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

Teşekkür

cevap

1

Sen bir fonksiyonu olarak all çağırmalıdır; aksi halde sadece bir satılabilir. yerine

matches = First.objects.all 

EDIT ait

matches = First.objects.all() 

: Aslında opponents almak için lütfen İlk örneğinin FK erişmesi gerekir.

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

Bkz. "İlk" örneklerin "rakip" veya "match_date" özelliklerine sahip olmaması; Yukarıda gösterildiği gibi ilgili "Fikstür" örneğine erişmelisiniz. –

+0

Üzgünüz, hala değişiklik yok –

+0

Ayrıca, şablonda "match.all" öğesini çağırmamaya dikkat edin; bunun yerine, sadece "eşleştirmeleri" kullanın. İlk örneklerden bir Queryset'e erişiyorsunuz. –