加入模板和视图的分页
Posted
技术标签:
【中文标题】加入模板和视图的分页【英文标题】:Join template and pagination of the views 【发布时间】:2013-06-04 08:45:47 【问题描述】:我知道,我重复自己说我是编程世界的新手......但我更愿意说出来,即使我认为我的问题很明显......:)
这是我的模板:
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"> </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 %
Brani.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&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"> </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 films
和def brani
。
对于% block titolo %
和其他我想更改的单词,如果是电影或brani 不是问题,我使用% if films %
或% if brani %
。
但我不知道如果我只想要一个函数分页,我必须如何从视图中的函数传递'films': pagine
或'brani': pagine
。
有人可以帮帮我吗?
【问题讨论】:
您真的认为粘贴两个 200 多行的块有助于理解您的问题吗?如果这些模板绝对相同,只需使用一个,将特定于模型的名称更改为中性的名称(例如objects
),然后在视图中使用相同的名称而不是 @987654333 中的特定于模型的名称@。下一步是将常见的视图代码片段移动到单独的函数中,然后从两个视图中使用这些函数。这是一个基本操作,因此您应该能够执行该操作(并且许多 IDE 可以为您执行此重构,请查找“提取方法”)
【参考方案1】:
您可以使用自 1.3 起提供的通用 ListViews 来减少代码重复
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"> </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 %
通过这种方式,您可以强制分离,以便在这些视图的模板中只表示不同视图之间的不同之处
参考:
https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-display/#listview https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin https://docs.djangoproject.com/en/dev/topics/pagination/【讨论】:
@user2295617,如果此答案对您有所帮助或通知您,您会考虑将其标记为正确吗?以上是关于加入模板和视图的分页的主要内容,如果未能解决你的问题,请参考以下文章