【问题标题】:Display many to many relation relations in the template在模板中显示多对多关系关系
【发布时间】:2014-03-29 03:41:11
【问题描述】:

首先,我将发布所有可能需要的代码。 我的模型

class topic(models.Model):
        learningObjectivesTopic = models.ManyToManyField(learningObjective, verbose_name = "Lernziel")
        topic = models.TextField(verbose_name = 'Thema')                         

class learningObjective(models.Model):
        learningObjectives = models.TextField(verbose_name = 'Lernziel')

我的看法:

@login_required(login_url='login')
def lernziel(request):
        return render(request, 'lernziel.html', {'topic': topic.objects.all(), 'todo': todoList.objects.all()})

@login_required(login_url='login')
def create_lernziel(request):
        neuesLernziel=learningObjective(learningObjectives=request.POST['Lernziel'])
        neuesLernziel.save()
        neuesLernziel_Topic=topic.objects.get(topic=request.POST['Thema'])
        neuesLernziel_Topic.learningObjectivesTopic.add(neuesLernziel)
        return render(request, 'lernziel.html', {'topic': topic.objects.all(), 'todo': todoList.objects.all()})

还有我正在使用的模板

<html lang="{{ LANGUAGE_CODE|default:"de-de" }}" >
<head>
<h1 align = "center">Lernziele</h1>
</head>

<body>

<form action="{% url 'create_lernziel' %}" method="post">
{% csrf_token %}
<br>Hallo Benutzer: {{ user.username }}</br>
Lernziel: <textarea name="Lernziel" rows="3" cols="45" ></textarea>
<p>
 <select name="Thema" size="5">
  {% for topic_ in topic %}
   <option>{{ topic_.topic }}</option>
  {% endfor %}
 </select>
</p>
<input type="submit" value="Absenden" />
</form>

{% comment %}
Here should be the table which displays learning objectives and the related topics
{% endcomment %}

</body>
</html>

好的,我知道我的问题有点奇怪,因为我发布的代码没有直接错误。但是我尝试了很多次来正确显示我想要的东西,但我就是不知道如何正确地做。我的目标是有 2 个列/标题:[学习目标] 和 [主题]。对于每一个学习目标,我都想有一个新的行。并且在每一行中,都可以显示更多相关的主题。如果您需要更多信息或希望我更具体地了解我的问题,请将其发布在 cmets 中:)

编辑我对这种结构的第一个想法是:我遍历学习目标,为每个目标创建一行,然后在该行中列出学习目标和主题。显然它不起作用。表格可能必须是动态的,这样我的想法才能奏效,或者我只是有一个错误的想法。

<table border="1">
<th>Lernziel</th>
<th>Thema</th>

{% for topic_ in topic %}

        {% for lObj in topic_.learningObjectivesTopic.all %}
                <tr><td>{{ lObj }}</td>
        {% endfor %}

        <td>{{ topic_.topic }}</td></tr>

{% endfor %}

</table>

【问题讨论】:

  • 我不知道为什么你在从数据库中获取 neuesLernziel_Topic 实例后仍然直接保存它 - 这似乎是你以前的版本遗留下来的,但毫无意义。
  • 哦,是的,你是对的,我会改的

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


【解决方案1】:

这里没有什么复杂的。你已经知道你可以通过my_objective.topic_set.all() 来从一个目标到它的相关主题。因此,您只需要在遍历模板中的每个主题时执行此操作(因为它是模板,所以您删除括号):

<table>
    {% for objective in objectives %}
    <tr>
      <td>{{ objective.learningObjectives }}</td>
      <td>{% for topic in objective.topic_set.all %}
             {{ topic.topic }}{% if forloop.last %},{% endif %}
          {% endfor %}</td>
    </tr>
    {% endfor %}
</table>

【讨论】:

【解决方案2】:

models.py

class learningObjective(models.Model):
        learningObjectives = models.TextField(verbose_name = 'Lernziel')

class topic(models.Model):
        learningObjectivesTopic = models.ManyToManyField(learningObjective, verbose_name = "Lernziel")
        topic = models.TextField(verbose_name = 'Thema')     

如果你想把话题放在前面,使用"或',即"learningObjective"

class topic(models.Model):
        learningObjectivesTopic = models.ManyToManyField("learningObjective", verbose_name = "Lernziel")
        topic = models.TextField(verbose_name = 'Thema')                         

class learningObjective(models.Model):
        learningObjectives = models.TextField(verbose_name = 'Lernziel')



<p>
 <select name="Thema" size="5">
  {% for topic_ in topic %}
   <option>{{ topic_.topic }}</option>
   {% for lo in topic_.learningObjectivesTopic.all %}
        <option>{{ lo.learningObjectives }}</option>
   {% endfor %}
  {% endfor %}
 </select>
</p>

如果你想从 learningObjective 中获取主题

lo = learningObjective.objects.all()[0]
lo.topic_set()

在模板中,去掉“()”,相信你已经知道如何在模板中使用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2018-04-22
    • 2011-07-03
    • 2014-02-02
    • 1970-01-01
    相关资源
    最近更新 更多