django模板和视图的问题

时间:2013-04-21 13:18:35

标签: django django-models django-templates django-views django-urls

大家好,大家先感谢您的帮助,我是编程方面的新手。 我想在配音演员(在Film.html中)创建一个链接并使用ID打开一个新页面(Attore.html),在那里只加载与该ID相关联的数据,但它不会这样做,因为它加载它们但我不明白错误在哪里。我不知道为什么不在Attore.html中收取CSS,而在Film.html中没有问题,因为它位于Base.html上,所以很奇怪。

这里的代码有点简化。

models.py:

from django.db import models

class Attore( models.Model ):
    nome = models.CharField( max_length=30 )
    cognome = models.CharField( max_length=30 )
    foto = models.CharField( max_length=100 )
    data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
    def __unicode__(self):
        return self.nome + " " + self.cognome + " " + self.foto
    class Meta:
        verbose_name_plural = "Attori"

class Film( models.Model ):
    titolo = models.CharField( max_length=39 )
    trama = models.CharField( max_length=1000 )
    locandina = models.CharField( max_length=100 )
    copertina = models.CharField( max_length=100 )
    data_inserimento = models.DateField( null=True, verbose_name="data d'inserimento" )
    attori = models.ManyToManyField( Attore )
    def __unicode__(self):
        return self.titolo + " " + self.trama + " " + self.locandina + " " + self.copertina
    class Meta:
        verbose_name_plural = "Film"

views.py:

from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from models import *

def film(request):
    film = Film.objects.order_by("titolo")
    return render_to_response('Film.html', { 'film': film, })

def film_attore(request, id):
    get_attore_id = get_object_or_404( Attore, pk=id )
    return render_to_response('Attore.html', { 'film': Film.objects.filter( attori=get_attore_id ), 'attor': get_attore_id })

urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',    
    (r'^Film$', 'Database.views.film'),
    (r'^Attore/(\d+)/$', 'Database.views.film_attore'),
)

模板:

Base.html:

<!DOCTYPE html>
<html>
<head>
  <title>{% block titolo %}Titolo{% endblock %}</title>
  <link href="../static/css/Default.css" media="screen" rel="stylesheet" type="text/css">
</head>
<body>
  {% block contenuto %}Contenuto{% endblock %}
</body>
</html>

Film.html:

{% extends "Base.html" %}

{% block titolo %}Film{% endblock %}

{% block contenuto %}
  {% for dato in film %}
    {% for attore in dato.attori.all %}
      <a href="/Database/Attore/{{ attore.id }}">{{ attore.nome }} {{ attore.cognome }}</a>
    {% endfor %}
  {% endfor %}
{% endblock %}

Attore.html:

{% extends "Base.html" %}

{% block titolo %}Attore{% endblock %}

{% block contenuto %}
  {% for dato in film %}
    {% for attore in dato.attori.all %}
      <h2>{{ attore.nome }} {{ attore.cognome }}</h2>
    {% endfor %}
  {% endfor %}
{% endblock %}

1 个答案:

答案 0 :(得分:0)

首先,你应该为你的css设置绝对路径,而不是相对路径,这就是为什么不加载。

然后,对我来说,看起来代码正在完全按照它所说的去做。在film_attore视图中,您将电影和演员列表传递给模板,但在模板中您不使用attori变量,而是迭代此演员的所有电影,然后查找并打印每部电影中所有演员的名单。

相关问题