2017-01-28 18 views
1

私は構築しているサイトのホームページ上で、私は2つのアプリケーション、ブログとポートフォリオからデータを取り込もうとしています。ブログはうまくいっていますが、ポートフォリオは表示されません。私はそれが私のモデルに外部キーを持っているからだと思います。サイトは主にブログであるため、このプロジェクト構造を選択しましたが、サイドバーに最近のポートフォリオ作業を表示することをホームページに求めています。ご覧のように、index.htmlテンプレートはPortfolio.portfolioモデルのすべての情報を必要としないため、一部のフィールドだけをインポートしようとしました。 データがアプリをトラバースできるように、テンプレートに外部キーの関係を含めるにはどうすればよいですか?テンプレート内のDjango外部キー

テンプレート -

{% for i in IndexPortfolio %} 
    <div class="col-xs-12 col-sm-6 thumb"> 
     <a class="thumbnail" href="{{ IndexPortfolio.get_absolute_url }}"> 
      <img class="img-responsive" src="{{ IndexPortfolio.cover_image.url }}" alt=""> 
      <p>{{ portfolio.title }}/{{ portfolio.client_name }}</p> 
     </a> 
    </div> 
{% endfor %} 

プロジェクト構造

mysite/ 
blog/ 
    templates/ 
     blog/ 
      blog_list.html 
      blog_detail.html 
     index.html 
     bio.html 
     resume.html 
    models.py 
    views.py 
portfolio/ 
    templates/ 
     portfolio_list.html 
     portfolio_detail.html 
    models.py 
    views.py 

ブログ/ models.py

from django.db import models 
from datetime import date 
from django.urls import reverse # Used to generate URLs by reversing the URL patterns 

class IndexPortfolio(models.Model): 
    title = models.ForeignKey('portfolio.Portfolio', related_name='ip_title') 
    client_name = models.ForeignKey("portfolio.Portfolio", related_name='ip_client_name') 
    post_date = models.ForeignKey("portfolio.Portfolio", related_name='ip_post_date') 
    cover_image = models.ForeignKey("portfolio.Portfolio", related_name='ip_cover_image') 

    class Meta: 
     ordering = ["post_date"] 
     verbose_name_plural = "Homepage Highlights - Best Portfolio Pieces" 

    def ip_get_absolute_url(self): 
    """ 
    Returns the url to access a particular portfolio piece instance. 
    """ 
     return reverse('portfolio-detail', args=[str(self.id)]) 

    def __str__(self): 
    """ 
     String for representing the Model object. 
    """ 
     return self.ip_title 
index.htmlを210

ブログ/ views.py

from django.shortcuts import render 
from django.views import generic 
from .models import Blog, IndexPortfolio 


def index(request): 
""" 
View function for home page of site. 
""" 
# Generate most recent blog and portfolio post 
    portfolio_list = IndexPortfolio.objects.all().order_by('-post_date')[0:6] 
    blog_list = Blog.objects.all().order_by('-post_date')[0:15] 
# Render the HTML template index.html with the data in the context variable. 

    return render(
     request, 
     'index.html', 
     context={'portfolio_list': portfolio_list, 
        'blog_list': blog_list, 
        } 
    ) 

ポートフォリオ/ models.py

from django.db import models 
from datetime import date 
from django.urls import reverse # Used to generate URLs by reversing the URL patterns 


    class Portfolio(models.Model): 
""" 
Model representing a portfolio piece. 
""" 
    title = models.CharField(max_length=200) 
    client_name = models.CharField(max_length=200, blank=True) 
    content = models.TextField(max_length=4000) 
    post_date = models.DateField(default=date.today) 
    cover_image = models.ImageField(null=True, blank=True) 
    image = models.ImageField(null=True, blank=True) 

    CLIENT_TYPES = (
     ('a', 'agency'), 
     ('p', 'personal project'), 
     ('f', 'freelance'), 
     ('n', 'nonprofit'), 
    ) 

    client_type = models.CharField(max_length=1, choices=CLIENT_TYPES, blank=True, default='p', help_text='Client type') 

    class Meta: 
     ordering = ["post_date"] 
     verbose_name_plural = "Portfolio Pieces" 

    def get_absolute_url(self): 
    """ 
    Returns the url to access a particular portfolio piece instance. 
    """ 
     return reverse('portfolio-detail', args=[str(self.id)]) 

    def __str__(self): 
    """ 
    String for representing the Model object. 
    """ 
     return self.title 
+0

index.htmlをポートフォリオリスト%} ...? – thebjorn

+0

すでにポートフォリオインスタンスを含むクエリセットをテンプレートコンテキストにプルしました。あなたはそれを正しく使う必要があります。 – trixn

答えて

0

のためにあなたのループ内でportfolio_listを使用しています。その後、クロスアプリモデルからのデータを表示しました。結果として、私がblog/models.pyで行っていたすべてのIndexPortfolioを削除することができました。なぜなら、他のアプリのモデルをインポートする正しい方法を見つけてから、その時点で不要だったからです。

ブログ/ views.py

from django.shortcuts import render 
from django.views import generic 
from .models import Blog 
from portfolio.models import Portfolio 


def index(request): 
    """ 
    View function for home page of site. 
    """ 
    # Generate most recent blog and portfolio post 
    portfolio_list = Portfolio.objects.all().order_by('-post_date')[0:6] 
    blog_list = Blog.objects.all().order_by('-post_date')[0:15] 
    # Render the HTML template index.html with the data in the context variable. 

    return render(
     request, 
     'index.html', 
     context={'portfolio_list': portfolio_list, 
        'blog_list': blog_list, 
       } 
    ) 

テンプレート - あなたが唯一のポートフォリオのように、おそらく `{%、テンプレートに` portfolio_list`公開しました

{% for portfolio in portfolio_list %} 
    <div class="col-xs-12 col-sm-6 thumb"> 
     <a class="thumbnail" href="{{ portfolio.get_absolute_url }}"> 
      <img class="img-responsive" src="{{ portfolio.cover_image.url }}" alt=""> 
      <p>{{ portfolio.title }}/{{ portfolio.client_name }}</p> 
     </a> 
    </div> 
{% endfor %} 
+0

私の答えはあなたを助けますか?私はあなたが提案するようにインポートを編集します。彼女があなたの問題を解決したら、答えを受け入れることができます。あなたを助けてうれしい – Wilfried

1

ブログ/ views.py:使用Portfolioモデル、portfolio_list

from django.shortcuts import render 
from django.views import generic 
from .models import Blog 
from portfolio.models import Portfolio 


def index(request): 
    """ 
    View function for home page of site. 
    """ 
    # Generate most recent blog and portfolio post 
    portfolio_list = Portfolio.objects.all().order_by('-post_date')[0:6] 
    blog_list = Blog.objects.all().order_by('-post_date')[0:15] 
    # Render the HTML template index.html with the data in the context variable. 

    return render(
     request, 
     'index.html', 
     context={'portfolio_list': portfolio_list, 
        'blog_list': blog_list, 
        } 
    ) 
を初期化します

テンプレート - index.htmlを:知っている、私は「portfolio.models」の輸入を追加し、構文の一部を周りに変更しなければならなかった、判明

{% for p in portfolio_list %} 
    <div class="col-xs-12 col-sm-6 thumb"> 
     <a class="thumbnail" href="{{ p.get_absolute_url }}"> 
      <img class="img-responsive" src="{{ p.cover_image.url }}" alt=""> 
      <p>{{ p.title }}/{{ p.client_name }}</p> 
     </a> 
    </div> 
{% endfor %} 
関連する問題