我如何在Django中另一个表的select id之前访问一个表

时间:2017-09-27 22:57:53

标签: css django

我是Django的新手,我正在遵循Django项目教程中的步骤并适应我的目标:创建调查问卷,但在选择测试ID后,我在访问另一个表中的数据时遇到问题。这是文件(对不起我的英文)。

models.p

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

class Prova(models.Model):
    idProva = models.CharField(max_length=5,primary_key=True,null=False)
    tipoProva = models.CharField(max_length=5,null=False)
    anoProva = models.CharField(max_length=4,null=False)
    def __str__(self):
        return self.idProva

class Usuario(models.Model):
    matriculaUsuario = models.CharField(max_length=12,primary_key=True,null=False)
    nomeUsuario = models.CharField(max_length=200,null=False)
    tipoUsuario = models.CharField(max_length=1,null=False)
    emailUsuario = models.EmailField(null=False)
    senhaUsuario = models.CharField(max_length=20,null=False)
    def __str__(self):
        return self.matriculaUsuario

class Questao(models.Model):
    idQuestao = models.CharField(max_length=7,primary_key=True,null=False)
    idProva = models.ForeignKey(Prova)
    areaQuestao = models.CharField(max_length=50,null=False)
    tipoQuestao = models.CharField(max_length=1,null=False)
    textoQuestao = models.CharField(max_length=500,blank=True,null=True)
    imagemQuestao = models.ImageField(null=True,blank=True)
    perguntaQuestao = models.CharField(max_length=200,blank=True,null=True)
    statusQuestao = models.CharField(max_length=1,null=False)
    def __str__(self):
        return self.idQuestao

class Opcao(models.Model):
    idOpcao = models.CharField(max_length=8,primary_key=True,null=False)
    idQuestao = models.ForeignKey(Questao)
    aOpcao = models.CharField(max_length=200,null=False)
    bOpcao = models.CharField(max_length=200,null=False)
    cOpcao = models.CharField(max_length=200,null=False)
    dOpcao = models.CharField(max_length=200,null=False)
    eOpcao = models.CharField(max_length=200,null=False)
    escolhidaOpcao = models.CharField(max_length=200,null=True,blank=True)
    def __str__(self):
        return self.idOpcao

class Historico(models.Model):
    idHistorico = models.CharField(max_length=10,primary_key=True,null=False)
    idProva = models.ForeignKey(Prova,null=False)
    matriculaUsuario = models.ForeignKey(Usuario,null=False)
    def __str__(self):
        return self.idHistorico

class Resposta(models.Model):
    idResposta = models.CharField(max_length=9,primary_key=True,null=False)
    idQuestao = models.ForeignKey(Questao)
    matriculaUsuario = models.ForeignKey(Usuario)
    tipoResposta = models.CharField(max_length=1,null=False)
    certaResposta = models.CharField(max_length=1)
    textoResposta = models.CharField(max_length=1000)
    def __str__(self):
        return self.idResposta

的index.html

<link rel="stylesheet" href="{% static 'polls/js/script.js' %}">
<link rel="stylesheet" type="text/css" href="{% static 'polls/estilo.css' %}" />
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form name="provaForm" class="provaForm" action="" method="post">
        {% csrf_token %}
        {% if lista_de_provas %}
            {% for prova in lista_de_provas %}
                <input name="provas" id="provas" type="radio" value="{{prova.idProva}}" onChange="selecionaProva()"> {{prova.tipoProva}} {{prova.anoProva}}
                {% if lista_de_questoes %}
                    {% for questao in lista_de_questoes %}

                    {% endfor %}
                {% else %}
                    {% if questao.idProva != NULL %}
                        <li>Questões não encontradas</li>
                    {% endif %}
                {% endif %}
            {% endfor %}
        {% else %}
            <li>Provas não encontradas</li>
        {% endif %}
        <input type="submit" value="Selecionar Prova" />
    </form>
</body>
</html>

请告诉我有什么问题。我需要使用javascript吗?

1 个答案:

答案 0 :(得分:0)

如果您需要使用questao定义的prova来获取与Questao.idProva相关的lista_de_questao = prova.questao_set.filter()列表,则可以使用{% if lista_de_provas %} {% for prova in lista_de_provas %} <input name="provas" id="provas" type="radio" value="{{prova.idProva}}" onChange="selecionaProva()"> {{prova.tipoProva}} {{prova.anoProva}} {% if prova.questao_set.exists %} {% for questao in prova.questao_set.filter %} {% endfor %} {% else %} {% if questao.idProva != NULL %} <li>Questões não encontradas</li> {% endif %} {% endif %} {% endfor %} {% else %} <li>Provas não encontradas</li> {% endif %}

在您的模板中,您可以通过以下方式更改它来执行此操作:

cumsum