私は構築しているサイトのホームページ上で、私は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
index.htmlをポートフォリオリスト%} ...? – thebjorn
すでにポートフォリオインスタンスを含むクエリセットをテンプレートコンテキストにプルしました。あなたはそれを正しく使う必要があります。 – trixn