gpt4 book ai didi

python - 连接模板和 View 分页

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

我知道,我重复一遍,我是编程世界的新手...但我更喜欢这样说,即使我认为从我的问题中显而易见...:)

这是我的模板:

Fims.html:

{% extends "Base.html" %}

{% block titolo %}Films{% endblock %}

{% block personalizzazione %}
<style media="screen" rel="stylesheet" type="text/css">
#Films {color:#FF0066; }
</style>
{% endblock %}

{% block testata %}
<div class="grid_16 bordoB1 padding5_0 center_p Lheight20_p h20_p bold_p">
<div class="grid_6">
<p class="bordoR marginR30">Umori</p>
</div>
<div class="grid_6">
<p class="bordoR bordoL3 marginL-30 marginR30">Generi</p>
</div>
<div class="grid_2">
<p class="bordoR bordoL3 marginL-30 marginR15">Dettagli</p>
</div>
<div class="grid_2">
<p class=" bordoL3 marginL-15">Utente</p>
</div>
</div>
{% endblock %}

{% block testata_a %}
<div class="grid_2 margin10_0">
<p class="bordoL4 marginL-30">
<a href="#" class="hover marginL30">Animazione</a><br>
<a href="#" class="hover marginL30">Avventura</a><br>
<a href="#" class="hover marginL30">Azione</a><br>
<a href="#" class="hover marginL30">Comici</a><br>
<a href="#" class="hover marginL30">Commedia</a><br>
<a href="#" class="hover marginL30">Documentari</a><br>
<a href="#" class="hover marginL30">Drammatici</a>
</p>
</div>
<div class="grid_2 margin10_0">
<p>
<a href="#" class="hover">Fantascienza</a><br>
<a href="#" class="hover">Fantasy</a><br>
<a href="#" class="hover">Fiabeschi</a><br>
<a href="#" class="hover">Gialli</a><br>
<a href="#" class="hover">Guerra</a><br>
<a href="#" class="hover">Horror</a><br>
<a href="#" class="hover">Musical</a>
</p>
</div>
<div class="grid_2 margin10_0">
<p class="bordoR marginR30">
<a href="#" class="hover">Noir</a><br>
<a href="#" class="hover">Polizieschi</a><br>
<a href="#" class="hover">Romantici</a><br>
<a href="#" class="hover">Thriller</a><br>
<a href="#" class="hover">Western</a><br>
<a href="#" class="hover">Proponi nuovo</a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a>
</p>
</div>
{% endblock %}

{% block testata_b %}
<div class="grid_2 margin10_0">
<p class="bordoL4 marginL-15">
<a href="#" class="hover marginL15">Consigliati per te</a><br>
<a href="#" class="hover marginL15">Piaciuti / Non piaciuti</a><br>
<a href="#" class="hover marginL15">Recensiti / Votati</a><br>
<a href="#" class="hover marginL15">Umore impostato</a><br>
<a href="#" class="hover marginL15">Segnala titolo assente</a><br>
<a href="#" class="hover marginL15">Visti</a><br>
<a href="#" class="hover marginL15">Top 10 / 50 / 100</a><br>
<a href="#" class="hover"></a>
<p></p>
</div>
{% endblock %}

{% block contenuto %}
{% for film in films %}
<div class="grid_4 marginB10 h205">
<div class="grid_2 Lheight10">
<a href="/Database/Film/{{ film.id }}"><img class="w120 h165 marginB5" src="Amore-e-altri-rimedi.jpg" ></a>
</div>
<div class="grid_2 Lheight10">
<p class="color marginL5 marginB10 size12">Visto:<span class="bold">No</span></p>
</div>
<div class="grid_2 Lheight10">
<p class="color marginL5 marginB10 size12">Umore:<span class="bold"></span></p>
</div>
<div class="grid_2 Lheight12 marginT5">
<p class="color marginL5 marginT-5 bordoB2 paddingB5 marginR10 size12">Voto: <img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"></p>
</div>
<div class="grid_2 Lheight12 marginT5">
<p class="marginL5 bold color marginB10">{% for umori in film.umori.all %}{{ umori.umore }}<br>{% endfor %}</p>
</div>
<div class="grid_2 Lheight12">
<p class="marginL5 bold color marginB10">{% for generi in film.generi.all %}{{ generi.genere }}<br>{% endfor %}</p>
</div>
<div class="grid_2 Lheight12">
<p class="marginL5 bold color marginB10">{{ film.anno }}</p>
</div>
<div class="grid_2 Lheight12">
<p class="bold color marginL5 marginB10">{{ film.durata }} min.</p>
</div>
<div class="clear">&nbsp;</div>
<div class="grid_2">
<h4><a class="black hover" href="/Database/Film/{{ film.id }}">{{ film.titolo }}</a></h4>
</div>
<div class="grid_2">
<p class="marginB10"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"></p>
</div>
</div>
{% endfor %}

<div class="grid_16 center">
{% if films.has_previous %}
<a class="color2 bold hover" href="?pagina={{ films.previous_page_number }}">precedente</a>
{% endif %}

{% for i in pagine %}
{% if i != films.number %}
<a class="black bold hover" href="?pagina={{ i }}">{{ i }}</a>
{% else %}
<a class="color1 bold line size20">{{ i }}</a>
{% endif %}
{% endfor %}

{% if films.has_next %}
<a class="color2 bold hover" href="?pagina={{ films.next_page_number }}">successivo</a>
{% endif %}
</div>
{% endblock %}

布拉尼.html

{% extends "Base.html" %}

{% block titolo %}Brani{% endblock %}

{% block personalizzazione %}
<style media="screen" rel="stylesheet" type="text/css">
#Brani {color:#FF0066; }
</style>
{% endblock %}

{% block testata %}
<div class="grid_16 bordoB1 padding5_0 center_p Lheight20_p h20_p bold_p">
<div class="grid_6">
<p class="bordoR marginR30">Umori</p>
</div>
<div class="grid_6">
<p class="bordoR bordoL3 marginL-30 marginR30">Generi</p>
</div>
<div class="grid_2">
<p class="bordoR bordoL3 marginL-30 marginR15">Dettagli</p>
</div>
<div class="grid_2">
<p class=" bordoL3 marginL-15">Utente</p>
</div>
</div>
{% endblock %}

{% block testata_a %}
<div class="grid_2 margin10_0">
<p class="bordoL4 marginL-30">
<a href="#" class="hover marginL30">Alternativa</a><br>
<a href="#" class="hover marginL30">Classica</a><br>
<a href="#" class="hover marginL30">Dance</a><br>
<a href="#" class="hover marginL30">Elettronica</a><br>
<a href="#" class="hover marginL30">Hip-Hop / Rap</a><br>
<a href="#" class="hover marginL30">Jazz</a><br>
<a href="#" class="hover marginL30">Pop</a>
</p>
</div>
<div class="grid_2 margin10_0">
<p>
<a href="#" class="hover">R&amp;B / Soul</a><br>
<a href="#" class="hover">Reggae</a><br>
<a href="#" class="hover">Rock</a><br>
<a href="#" class="hover">Colonne sonore</a><br>
<a href="#" class="hover">World</a><br>
<a href="#" class="hover">Proponi nuovo</a><br>
<a href="#" class="hover"></a>
</p>
</div>
<div class="grid_2 margin10_0">
<p class="bordoR marginR30">
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a>
</p>
</div>
{% endblock %}

{% block testata_b %}
<div class="grid_2 margin10_0">
<p class="bordoL4 marginL-15">
<a href="#" class="hover marginL15">Consigliati per te</a><br>
<a href="#" class="hover marginL15">Piaciuti / Non piaciuti</a><br>
<a href="#" class="hover marginL15">Recensiti / Votati</a><br>
<a href="#" class="hover marginL15">Umore impostato</a><br>
<a href="#" class="hover marginL15">Segnala titolo assente</a><br>
<a href="#" class="hover marginL15">Ascoltati</a><br>
<a href="#" class="hover marginL15">Top 10 / 50 / 100</a><br>
<a href="#" class="hover"></a>
<p></p>
</div>
{% endblock %}

{% block contenuto %}
{% for brano in brani %}
<div class="grid_4 marginB10 h205">
<div class="grid_2 Lheight10">
<a href="/Database/Brano/{{ brano.id }}"><img class="w120 h165 marginB5" src="Amore-e-altri-rimedi.jpg" ></a>
</div>
<div class="grid_2 Lheight10">
<p class="color marginL5 marginB10 size12">Ascoltato:<span class="bold">No</span></p>
</div>
<div class="grid_2 Lheight10">
<p class="color marginL5 marginB10 size12">Umore:<span class="bold"></span></p>
</div>
<div class="grid_2 Lheight12 marginT5">
<p class="color marginL5 marginT-5 bordoB2 paddingB5 marginR10 size12">Voto: <img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"></p>
</div>
<div class="grid_2 Lheight12 marginT5">
<p class="marginL5 bold color marginB10">{% for umori in brano.umori.all %}{{ umori.umore }}<br>{% endfor %}</p>
</div>
<div class="grid_2 Lheight12">
<p class="marginL5 bold color marginB10">{% for generi in brano.generi.all %}{{ generi.genere }}<br>{% endfor %}</p>
</div>
<div class="grid_2 Lheight12">
<p class="marginL5 bold color marginB10">{{ brano.anno }}</p>
</div>
<div class="grid_2 Lheight12">
<p class="bold color marginL5 marginB10">{{ brano.durata }} min.</p>
</div>
<div class="clear">&nbsp;</div>
<div class="grid_2">
<h4><a class="black hover" href="/Database/Brano/{{ brano.id }}">{{ brano.titolo }}</a></h4>
</div>
<div class="grid_2">
<p class="marginB10"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"></p>
</div>
</div>
{% endfor %}
<div class="grid_16 center">
{% if brani.has_previous %}
<a class="color2 bold hover" href="?pagina={{ brani.previous_page_number }}">precedente</a>
{% endif %}

{% for i in pagine %}
{% if i != brani.number %}
<a class="black bold hover" href="?pagina={{ i }}">{{ i }}</a>
{% else %}
<a class="color1 bold line size20">{{ i }}</a>
{% endif %}
{% endfor %}

{% if brani.has_next %}
<a class="color2 bold hover" href="?pagina={{ brani.next_page_number }}">successivo</a>
{% endif %}
</div>
{% endblock %}

这是我的观点:

def films(request):
films = Film.objects.order_by("anno", "titolo")
paginator = Paginator( films, 2 )
try:
pagina = int( request.GET.get( "pagina", "1" ) )
except ValueError:
pagina = 1
try:
pagine = paginator.page( pagina )
except ( EmptyPage, InvalidPage ):
pagine = paginator.page ( paginator.num_pages )
return render_to_response('Films.html', { 'films': pagine, 'pagine': range( 1, paginator.num_pages + 1 ) } )

def brani(request):
brani = Brano.objects.order_by("anno", "titolo")
paginator = Paginator( brani, 2 )
try:
pagina = int( request.GET.get( "pagina", "1" ) )
except ValueError:
pagina = 1
try:
pagine = paginator.page( pagina )
except ( EmptyPage, InvalidPage ):
pagine = paginator.page ( paginator.num_pages )
return render_to_response('Brani.html', { 'brani': pagine, 'pagine': range( 1, paginator.num_pages + 1 ) } )

我只想有一个模板,因为它们实际上是相同的,我还想创建一个 def Pagination 来调用 def film def brani.

对于 {% block titolo %} 和我想更改的其他单词(如果是电影或brani 不是问题),我使用 {% iffilms %}{% if brani %}

但如果我只想要一个,我不知道如何从 View 中的函数传递 'films': pagine'brani': pagine两者的功能分页。有人可以帮助我吗?

最佳答案

您可以使用 1.3 版以来可用的通用 ListView 来减少代码重复

views.py
from django.views.generic.list import ListView

class FilmListView(ListView):
model = Film
paginate_by = 10
page_kwarg = 'pagina'
context_object_name = "films"

class BranoListView(ListView):
model = Brano
paginate_by = 10
page_kwarg = 'pagina'
context_object_name = "brani"

然后您可以设置通用分页模板

分页.html

{% extends "base.html" %}

{% block contenuto %}
{% block pagina %}
<div class="grid_16 center">
{% if objects.has_previous %}
<a class="color2 bold hover" href="?pagina={{ objects.previous_page_number }}">precedente</a>
{% endif %}

{% for i in paginator.page_range %}
{% if i == page.number %}
<a class="color1 bold line size20">{{ i }}</a>
{% else %}
<a class="black bold hover" href="?pagina={{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}

{% if objects.has_next %}
<a class="color2 bold hover" href="?pagina={{ objects.next_page_number }}">successivo</a>
{% endif %}
</div>
{% block pagina %}
{% endblock %}

电影.html

{% extends "pagination.html" %}

{% block titolo %}Films{% endblock %}

{% block personalizzazione %}
<style media="screen" rel="stylesheet" type="text/css">
#Films {color:#FF0066; }
</style>
{% endblock %}

{% block testata %}
<div class="grid_16 bordoB1 padding5_0 center_p Lheight20_p h20_p bold_p">
<div class="grid_6">
<p class="bordoR marginR30">Umori</p>
</div>
<div class="grid_6">
<p class="bordoR bordoL3 marginL-30 marginR30">Generi</p>
</div>
<div class="grid_2">
<p class="bordoR bordoL3 marginL-30 marginR15">Dettagli</p>
</div>
<div class="grid_2">
<p class=" bordoL3 marginL-15">Utente</p>
</div>
</div>
{% endblock %}

{% block testata_a %}
<div class="grid_2 margin10_0">
<p class="bordoL4 marginL-30">
<a href="#" class="hover marginL30">Animazione</a><br>
<a href="#" class="hover marginL30">Avventura</a><br>
<a href="#" class="hover marginL30">Azione</a><br>
<a href="#" class="hover marginL30">Comici</a><br>
<a href="#" class="hover marginL30">Commedia</a><br>
<a href="#" class="hover marginL30">Documentari</a><br>
<a href="#" class="hover marginL30">Drammatici</a>
</p>
</div>
<div class="grid_2 margin10_0">
<p>
<a href="#" class="hover">Fantascienza</a><br>
<a href="#" class="hover">Fantasy</a><br>
<a href="#" class="hover">Fiabeschi</a><br>
<a href="#" class="hover">Gialli</a><br>
<a href="#" class="hover">Guerra</a><br>
<a href="#" class="hover">Horror</a><br>
<a href="#" class="hover">Musical</a>
</p>
</div>
<div class="grid_2 margin10_0">
<p class="bordoR marginR30">
<a href="#" class="hover">Noir</a><br>
<a href="#" class="hover">Polizieschi</a><br>
<a href="#" class="hover">Romantici</a><br>
<a href="#" class="hover">Thriller</a><br>
<a href="#" class="hover">Western</a><br>
<a href="#" class="hover">Proponi nuovo</a><br>
<a href="#" class="hover"></a><br>
<a href="#" class="hover"></a>
</p>
</div>
{% endblock %}

{% block testata_b %}
<div class="grid_2 margin10_0">
<p class="bordoL4 marginL-15">
<a href="#" class="hover marginL15">Consigliati per te</a><br>
<a href="#" class="hover marginL15">Piaciuti / Non piaciuti</a><br>
<a href="#" class="hover marginL15">Recensiti / Votati</a><br>
<a href="#" class="hover marginL15">Umore impostato</a><br>
<a href="#" class="hover marginL15">Segnala titolo assente</a><br>
<a href="#" class="hover marginL15">Visti</a><br>
<a href="#" class="hover marginL15">Top 10 / 50 / 100</a><br>
<a href="#" class="hover"></a>
<p></p>
</div>
{% endblock %}

{% block contenuto %}
{{ block.super }}{# This will insert the pagination here #}
{% for film in films %}
<div class="grid_4 marginB10 h205">
<div class="grid_2 Lheight10">
<a href="/Database/Film/{{ film.id }}"><img class="w120 h165 marginB5" src="Amore-e-altri-rimedi.jpg" ></a>
</div>
<div class="grid_2 Lheight10">
<p class="color marginL5 marginB10 size12">Visto:<span class="bold">No</span></p>
</div>
<div class="grid_2 Lheight10">
<p class="color marginL5 marginB10 size12">Umore:<span class="bold"></span></p>
</div>
<div class="grid_2 Lheight12 marginT5">
<p class="color marginL5 marginT-5 bordoB2 paddingB5 marginR10 size12">Voto: <img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"><img class="h10 w10" src="nessuno.jpg"></p>
</div>
<div class="grid_2 Lheight12 marginT5">
<p class="marginL5 bold color marginB10">{% for umori in film.umori.all %}{{ umori.umore }}<br>{% endfor %}</p>
</div>
<div class="grid_2 Lheight12">
<p class="marginL5 bold color marginB10">{% for generi in film.generi.all %}{{ generi.genere }}<br>{% endfor %}</p>
</div>
<div class="grid_2 Lheight12">
<p class="marginL5 bold color marginB10">{{ film.anno }}</p>
</div>
<div class="grid_2 Lheight12">
<p class="bold color marginL5 marginB10">{{ film.durata }} min.</p>
</div>
<div class="clear">&nbsp;</div>
<div class="grid_2">
<h4><a class="black hover" href="/Database/Film/{{ film.id }}">{{ film.titolo }}</a></h4>
</div>
<div class="grid_2">
<p class="marginB10"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"><img class="h20 w20 marginT-4" src="stella.jpg"></p>
</div>
</div>
{% endfor %}
{{ block.super }}{# This will insert the pagination here #}
{% endblock %}

通过这种方式,您可以强制分离,以便在这些 View 的模板中只表示不同 View 之间不同的内容

引用:

关于python - 连接模板和 View 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16994341/

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