如何访问 ManyToManyField 对象的 id?

时间:2021-05-06 00:31:09

标签: django django-models django-views manytomanyfield

我的模型:

class Aula(models.Model):
    
    nome_aula = models.CharField(max_length=255)
    descricao_aula = models.TextField()
    
    ...
    
    def __str__(self):
        return self.nome_aula

class Modulos(models.Model):

    numero_modulo = models.CharField(default="Módulo 1",max_length=400)

    aulas = models.ManyToManyField(Aula)

    def __str__(self):
        return self.numero_modulo

我的观点:

def aulasdomodulo(request, id):

    mod = get_object_or_404(Modulos, pk=id)

    aulas = mod.aulas.all()


    return render(request, 'wtic/aulasdomodulo.html', {'aulas':aulas, 'mod':mod})

def conteudodaaula(request, id):

    mod2 = get_object_or_404(Modulos, pk=id)

    aula = mod2.aulas.all()

    ...

    return render(request,'wtic/conteudo_aula.html', {'aula':aula})

我的 html 显示分配给该模块访问它们的对象

{% for aula in aulas %}

{{aula.nome_aula}}
<a href="/aula/{{aula.id}}">Acessar</a>

{% endfor %}

enter image description here

但是当我尝试访问课程时,我明白了

enter image description here

如何每页访问一个这些对象?

0 个答案:

没有答案