gpt4 book ai didi

Python Django 如何无法从 html 文件上的 views.py 呈现我的变量

转载 作者:行者123 更新时间:2023-11-28 02:23:03 24 4
gpt4 key购买 nike

我如何从 html 文件中的模型渲染我的变量,在我用 class(DetailView) 在我的 views.py 中制作 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:&ensp;{{ product.processor }}<br>
<br>
Graphic Card:&ensp;{{ product.video }}<br>
<br>
Memory:&ensp;{{ product.ram }}<br>
<br>
Disk Space:&ensp;{{ product.disk_space }}<br>
<br>
OS:&ensp;{{ 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.___ }} 调用的 Product 变量,但不会像我使用以下链接调用的那样呈现其余部分:{% for link in links %} {{ link.link }}{% endfor %} 或者喜欢我的文章...

我应该如何继续在我的页面上呈现所需的模型。

Image of file paths here

最佳答案

使用 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,而不是声明“def links(self)”:

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

关于Python Django 如何无法从 html 文件上的 views.py 呈现我的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48098278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com