1

我知道,我重复自己说我是编程世界的新手……但我更愿意说出来,即使我认为我的问题很明显……:)

这是我的模板:

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 %}

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&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 ) } )

我只想拥有一个模板,因为它们实际上是相同的,而且我还想创建一个使用anddef Pagination调用的模板。def filmsdef brani

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

但是如果我只想要一个功能分页,我不知道如何通过视图中的功能'films': pagine'brani': pagine从视图中的功能。有人可以帮助我吗?

4

1 回答 1

1

您可以使用自 1.3 起提供的通用 ListViews 来减少代码重复

视图.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 %}

通过这种方式,您可以强制分离,以便在这些视图的模板中仅表示不同视图之间的不同之处

参考:

于 2013-06-08T02:36:18.570 回答