django模板和视图的问题

时间:2021-07-11 20:12:04

Hello everyone thanks in advance on the aid, I'm new in programming in general. I would like to create a link in the voice actor (in Film.html) and using the ID to open a new page (Attore.html) where to load only the data associated with that ID but it doesn't do that since it loads them all but I don't understand where it's the error. I don't also know why not charge the CSS in Attore.html, while in Film.html there are no problems and it is strange since it is located on Base.html.

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

Here is the code a little simplified.

这里的代码有点简化。

models.py :

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 :

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

urls.py

from django.conf.urls.defaults import *

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

Template:

模板:

Base.html :

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 :

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 :

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 个解决方案

#1


0  

First thing is you should put an absolute path to your css, not a relative one, that's why is not loading.

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

Then, to me it looks like the code is doing exactly what it's told. In the film_attore view you pass the list of movies and the actor to the template but in the template you don't use the attori variable and instead iterate over all movies for this actor then lookup and print the list of all actors in each movie.

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

#1


0  

First thing is you should put an absolute path to your css, not a relative one, that's why is not loading.

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

Then, to me it looks like the code is doing exactly what it's told. In the film_attore view you pass the list of movies and the actor to the template but in the template you don't use the attori variable and instead iterate over all movies for this actor then lookup and print the list of all actors in each movie.

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