gpt4 book ai didi

python - django 1.8 View 中的循环

转载 作者:太空宇宙 更新时间:2023-11-03 17:39:57 25 4
gpt4 key购买 nike

我有一个models.py我用 3 ImageFields 指定一个类和 3 CharFields 。 CharFields 包含我想在 detail.html 中使用的 YouTube 视频 ID。 。但我不知道应该如何引用 details.html 中的元素

我的models.py

from django.db import models

class Feat(models.Model):
feat_name = models.CharField(max_length=200,default="feature1")
feat_img1 = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
feat_img2 = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
feat_img3 = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
feat_vid1 = models.CharField(max_length=200,default="3NQRhE772b0")
feat_vid2 = models.CharField(max_length=200,default="XU3h3CVI_gI")
feat_vid3 = models.CharField(max_length=200,default="vqHIQD4_lu4")
num = models.IntegerField(default=0)

def __str__(self):
return self.feat_name

view.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Feat


class IndexView(generic.ListView):
template_name = 'feature/base.html'
context_object_name = 'latest_feat_list'

def get_queryset(self):
return Feat.objects.order_by('-num')[:1]

class DetailView(generic.DetailView):
model = Feat
template_name = 'feature/detail.html'

base.html

{% if latest_feat_list %}
<ul>
{% for feat in latest_feat_list %}
<a href="{% url 'feature:detail' feat.id %}"><img src="{{ feat.feat_img1.url }}" alt="ac/dc">
<a href="{% url 'feature:detail' feat.id %}"><img src="{{ feat.feat_img2.url }}" alt="ac/dc">
<a href="{% url 'feature:detail' feat.id %}"><img src="{{ feat.feat_img3.url }}" alt="ac/dc">
{% endfor %}
</ul>
{% else %}
<p>No </p>
{% endif %}

detail.html

{% for `condition` %}
<img id="hide" src="http://img.youtube.com/vi/{{ choice.feat_vid1 }}/hqdefault.jpg" data-video="https://www.youtube.com/embed/{{ choice.feat_vid1 }}?autoplay=1" width="480" height="300"/>
<img id="hide" src="http://img.youtube.com/vi/{{ choice.feat_vid2 }}/hqdefault.jpg" data-video="https://www.youtube.com/embed/{{ choice.feat_vid2 }}?autoplay=1" width="480" height="300"/>
<img id="hide" src="http://img.youtube.com/vi/{{ choice.feat_vid3 }}/hqdefault.jpg" data-video="https://www.youtube.com/embed/{{ choice.feat_vid3 }}?autoplay=1" width="480" height="300"/>


{% endfor %}

我的申请urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='base'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

]

我需要显示Feat中传入的所有id类(class) detail.html但不知道如何引用 Feat 中的元素.

最佳答案

我尝试过实现

该功能是显示三个图像,当用户单击其中任何一个图像时,将显示与该图像相关的视频。我确信它可以更有效地完成,但我才刚刚开始,这似乎没问题。帮助我吗?

这与你所做的有点不同,但仍然......我不会描述代码,因为它会花费很长时间,但我很乐意回答您的任何问题。另外我强烈建议您阅读有关 django 模型的文档。

models.py:

from django.db import models


class Feat(models.Model):
title = models.CharField(max_length=150)
image = models.ImageField(upload_to='path/')
youtube_link = models.URLField()
created = models.DateTimeField(auto_now_add=True)

class Meta:
verbose_name = "Feat"
verbose_name_plural = "Feats"

def __str__(self):
return self.title

views.py:

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Feat


class IndexView(generic.ListView):
template_name = 'feature/index.html'
context_object_name = 'latest_feats'

def get_queryset(self):
return Feat.objects.order_by('-created')

class DetailView(generic.DetailView):
model = Feat
template_name = 'feature/detail.html'
context_object_name = 'feat'

index.html:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% if latest_feats %}
<ul>
{% for feat in latest_feats %}
<a href="{% url 'feature:detail' feat.id %}"><img src="{{ feat.image.url }}" alt="some text"></a>
{% endfor %}
</ul>
{% else %}
<p>No </p>
{% endif %}
</body>
</html>

detail.html:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<iframe width="560" height="345" src="{{ feat.youtube_link }}" frameborder="0" allowfullscreen></iframe>
</body>
</html>

urls.py:

urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]

有很多不美好的事情。例如,实现模板继承会更好,但我认为目前这些就足够了。

关于python - django 1.8 View 中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704987/

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