2013-03-02 53 views
0
This is my models.py file 


from django.db import models 

    class Author(models.Model): 
     first_name = models.CharField(max_length=30) 
     last_name = models.CharField(max_length=40) 
     email = models.EmailField() 
     age=models.IntegerField() 

     class Meta: 
     db_table=u'Author Info' 

     def __unicode__(self): 
      return u"%d %s %s %s" % (self.pk, self.name, self.author.name, self.publisher_name) 

     def books(self): 
      return Book.objects.filter(author=self) 

    class Book(models.Model): 
     book_name=models.CharField(max_length=30) 
     publisher_name=models.CharField(max_length=40) 
     author=models.ForeignKey(Author) 

     class Meta: 
      db_table = u'Book Name' 

     def __unicode__(self): 
      return u'%d %s %s' % (self.pk, self.first_name, self.last_name) 

我想從兩個表diaplay的DATAS,在django.Please指導我如何寫views.py和templates.i正在使用作者表的外鍵從Django中的外鍵關係檢索兩個表中的數據?

+0

這是你的問題早 – catherine 2013-03-02 10:11:53

+0

這是所有教程解釋的很清楚。你有什麼問題? – 2013-03-02 11:05:22

回答

1

以下幾點看法和HTML被用於顯示所有具有相應作者詳細信息的書籍。

views.py

def client_add(request): 
    books = Book.objects.all() 
    return render_to_response('book_details.html', locals(), context_instance=RequestContext(request)) 

book_details.html

<body> 
{% for book in books %} 
{{book.book_name}} 
{{book.publisher_name}} 
{{book.author.first_name}} 
{{book.author.last_name}} 
{{book.author.email}} 
{{book.author.age}} 
{% endif %} 
</body> 

以下幾點看法和HTML用於顯示書籍特定作者corrosponding細節。

views.py

def client_add(request): 
    books = Book.objects.all(author_last_name ="author_last_name") 
    return render_to_response('book_details.html', locals(), context_instance=RequestContext(request)) 

book_details.html

<body> 
{% for book in books %} 
{{book.book_name}} 
{{book.publisher_name}} 
{{book.author.first_name}} 
{{book.author.last_name}} 
{{book.author.email}} 
{{book.author.age}} 
{% endif %} 
</body>