我如何从html文件中的模型渲染我的变量,他们是在我在views.py
中使用class(DetailView)
制作html链接slug之前提前渲染的
HTML FILE(product.html)
<h2 class="heading">{{ product.name }}</h2>
<div style="clear: both;"><br></div>
<div class="block">
<div class="img_preview">
<img src="{{ product.img }}">
<div class="categ">
{% for category in product.category.all %}
<a href=""><span>{{ category }}</span></a>
{% endfor %}
{# <div style="clear: both"><br></div> #}
</div>
</div>
<div id="links">
{% for link in links %}
<div class="download_link">
<a href="{{ links.link }}"><span>DOWNLOAD MIRROR NUMBER {{ links.number }}</span></a>
</div>
{% endfor %}
</div>
<div style="clear: both"></div>
<div id="games">
<div id="video_trailer">
<iframe width="560" height="317" src="{{ product.video_trailer }}" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>
<div id="description">
<span class="title">Description</span>
<p>{{ product.description }}</p>
</div>
</div>
<div style="clear: both"><br></div>
<div id="system_requirements">
<span class="title">System Requirements</span><br>
<p>Processor: {{ product.processor }}<br>
<br>
Graphic Card: {{ product.video }}<br>
<br>
Memory: {{ product.ram }}<br>
<br>
Disk Space: {{ product.disk_space }}<br>
<br>
OS: {{ product.oS }}
</p>
</div>
</div>
<div id="screenshots">
{% for image in product_images %}
<div class="screenshot">
<img width="1920px" height="1080px" src="{{ image.image }}" >
</div>
{% endfor %}
</div>
</div>
</div>
<aside>
<div id="news">
<h2 class="heading">News</h2>
<div style="clear: both"><br></div>
{% for articles in news_articles %}
<div id="articles">
<div class="article">
<a href="{{ articles.article.get_absolute_url }}">
<img src="{{ articles.image }}">
<div style="clear: both"></div>
<span></span><div style="clear: both"></div>
</a>
<em>{{ articles.article.created }}</em>
views.py
from django.shortcuts import render
from products.models import *
from news.models import *
from django.shortcuts import get_object_or_404
from django.views.generic.detail import DetailView
class GameLink(DetailView):
# model = Product
# context_object_name = 'product'
def get_object(self):
return get_object_or_404(Product, slug__iexact=self.kwargs['slug'])
class ArticleLink(DetailView):
model = Article
context_object_name = 'article'
def get_object(self):
return get_object_or_404(Article, slug__iexact=self.kwargs['slug'])
def product(request, product_id):
product = Product.objects.get(id=product_id)
product_images = ProductImage.objects.filter(is_active=True, is_main=False, id=product_id)
links = ProductDownload.objects.filter(is_active=True, product=product_id)
news_articles = NewsImage.objects.filter(is_active=True, is_main=True)
return render(request, 'products/product.html', locals())
urls.py
from django.contrib import admin
from django.conf.urls import *
from products import views
from products.views import *
urlpatterns = [
# url(r'^games/(?P<product_id>\w+)/$', views.product, name='product'),
url(r'^games/(?P<slug>[-\w]+)/$', GameLink.as_view(template_name = 'products/product.html'), name='product'),
url(r'^articles/(?P<slug>[-\w]+)/$', ArticleLink.as_view(template_name = 'news/article.html'), name='article')
]
models.py
`
from django.db import models
from django.urls import reverse
class ProductCategory(models.Model):
name = models.CharField(max_length=128, blank=True, null=True, default=None)
is_active = models.BooleanField(default=True)
def __str__(self):
return '%s' % self.name
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
class Product(models.Model):
name = models.CharField(max_length=128, blank=True, null=True, default=None)
description = models.TextField(default=None)
processor = models.CharField(max_length=300, blank=True, null=True, default=None)
video = models.CharField(max_length=300, blank=True, null=True, default=None)
ram = models.CharField(max_length=300, blank=True, null=True, default=None)
disk_space = models.CharField(max_length=300, blank=True, null=True, default=None)
oS = models.CharField(max_length=300, blank=True, null=True, default=None)
video_trailer = models.CharField(max_length=10000, blank=True, null=True, default=None)
img = models.CharField(max_length=10000, blank=True, null=True, default=None)
category = models.ManyToManyField(ProductCategory, blank=True, default=None)
is_active = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(primary_key=True, max_length=250, unique=True, default=None)
def __str__(self):
return '%s' % self.name
def get_absolute_url(self):
return reverse('product', args=[str(self.slug)])
class Meta:
verbose_name = 'Game'
verbose_name_plural = 'Games'
class ProductDownload(models.Model):
product = models.ForeignKey(Product, blank=True, null=True, default=None, on_delete=False)
link = models.CharField(max_length=10000, blank=True, null=True, default=None)
is_active = models.BooleanField(default=True)
number = models.PositiveIntegerField(blank=True, default=True)
def __str__(self):
return '%s' % self.product.name
class Meta:
ordering = ['number']
class Meta:
verbose_name = 'Download Link'
verbose_name_plural = 'Download Links'
class ProductImage(models.Model):
product = models.ForeignKey(Product, blank=True, null=True, default=None, on_delete=False)
image = models.CharField(max_length=10000, blank=True, null=True, default=None)
is_main = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
def __str__(self):
return '%s' % self.product
class Meta:
verbose_name = 'Image'
verbose_name_plural = 'Images'
HTML文件仅显示使用{{ product.___ }}
调用的产品变量,但不会像我调用的链接一样呈现其余部分:
{% for link in links %}
{{ link.link }}
{% endfor %}
或者喜欢我的文章...
我应该如何在我的页面上渲染所需的模型。
文件路径的图像 here
答案 0 :(得分:0)
有不同的形式可以添加adicional&#39;变量&#39;使用DetailView (link)
例如:
class GameLink(DetailView):
def get_object(self):
return get_object_or_404(Product, slug__iexact=self.kwargs['slug'])
def links(self):
return ProductDownload.objects.filter(is_active=True, product=self.object)
在product.html中:
{% for link in view.links %}
<div class="download_link">
<a href="{{ link.link }}"><span>DOWNLOAD MIRROR NUMBER {{ links.number }}</span></a>
</div>
{% endfor %}
如果您不想更改HTML,而不是声明&#34; def链接(自我)&#34;:
class GameLink(DetailView):
def get_object(self):
return get_object_or_404(Product, slug__iexact=self.kwargs['slug'])
def get_context_data(self, **kwargs):
context = super(GameLink, self).get_context_data(**kwargs)
context['links'] = ProductDownload.objects.filter(is_active=True, product=self.object)
return context