希望这是一个快速/简单的方法.我知道通过自定义模板标签来解决这个问题的方法,但我很好奇是否有其他方法我在看.我为我的博客创建了各种各样的画廊功能,并且我有一个画廊列表页面,用于分割我的所有画廊.现在,我不想显示该列表中每个图库的所有照片,因为如果每个图库甚至有20个图像,那么如果我在5个帖子中分页,则该页面上有100个图像.那是浪费,而且是错误的做事方式.
我的问题是,有没有办法只显示照片集中的3张照片?我想做什么,但我认为不可能是(伪代码):
{% for photos in gallery.photo_set %}
{% if forloop.counter lt 3 %}
<img src="{{ photos.url }}">
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
从文档来看,除非我完全错过它,否则通过模板系统是不可能的.因此,我可以编写自己的各种模板标签来解决它.我可以从视角来做一些事情,但我没有深入研究这个想法.我的另一个选项是为模型提供预览字段,并允许用户在预览字段中选择他们想要的照片.
无论如何,有几个不同的选择,所以我想我会对观众进行调查,看看你是怎么做到的.任何意见表示赞赏.就个人而言,享受这种猫的皮肤有很多方法.
我想在我的代码中放弃并继续,但它在Django模板中不起作用.如何使用Django模板for循环使用continue和break.这是一个例子:
{% for i in i_range %}
{% for frequency in patient_meds.frequency %}
{% ifequal frequency i %}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}" checked/> {{ i }} AM</td>
{{ forloop.parentloop|continue }} ////// It doesn't work
{ continue } ////// It also doesn't work
{% endifequal %}
{% endfor%}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}"/> {{ i }} AM</td>
{% endfor %}
Run Code Online (Sandbox Code Playgroud) 我的编码是:观点
def showThread(request, thread_id)
post_list = Post.objects.filter(id = thread_id)
post_likes = PostLikes.objects.all()
return render_to_response('show.html',locals(),context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
楷模:
class Post(models.Model):
subject = models.CharField(max_length = 250)
body = models.TextField()
thread = models.ForeignKey('self', null = True, editable = False )
Run Code Online (Sandbox Code Playgroud)
Show.html:
{% for post in post_list %}
{{post.id}}{{post.subject}}
{% endfor %}
{% for post_like in post_likes %}
{% if post_like.post_id == post.id and post_like.user_id == user.id %}
U like this post{{post}}
{% else %}
{{post}}
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
在show.html,else部分中,它会一次又一次地显示值.但我只需要一次.当我进入其他条件时,我怎么能打破for循环.请帮助我..