我有一个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
中显示detail.html
类中传递的所有ID,但不知道如何引用Feat
中的元素。
答案 0 :(得分:1)
我已尝试实施
The function is to display three images and when the user clicks over any of them a video related to that image is displayed. I'm sure it can be done more efficiently but I've just started out and this seemed alright.Help me out?
它与你所做的有点不同,但仍然...... 我不会描述代码,因为它需要很长时间,但我很乐意回答你的任何问题。另外,我强烈建议你阅读关于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'),
]
有许多不美观的事情。例如,实现模板继承会更好,但我认为现在这些就足够了。