我知道,我重复自己说我是编程领域的新人......但我更喜欢说它,即使我认为从我的问题中可以看出......:)
这是我的模板:
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 %}
以及我要更改的其他字词,如果电影或分支不是问题,我会使用{% if films %}
或{% if brani %}
。
但我不知道如果我只需要一个函数分页,我必须从视图中的函数传递'films': pagine
或'brani': pagine
。
有人可以帮我吗?
答案 0 :(得分:1)
您可以使用自1.3以来可用的通用ListView来减少代码重复。
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"
然后您可以设置通用分页模板
{% 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 %}
{% 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 %}
通过这种方式,您可以强制执行分离,以便在这些视图的模板中仅显示不同视图之间不同的内容
REF: