首先,我将发布所有可能必要的代码。 我的模特:
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列/标题:[学习目标]和[主题]。对于每个学习目标,我想要一个新的行。在每一行中,都可以显示更多相关主题。如果您需要更多信息或希望我更具体地解决我的问题,请在评论中发布:)
编辑我首先想到的是那个结构:我遍历学习目标,为每个目标创建一行,然后列出该行中的学习目标和主题。显然它不起作用。表可能必须是动态的,这样我的想法才有用,或者我只是想错了。
<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>
答案 0 :(得分:2)
这里没有什么复杂的。您已经知道,您可以通过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>
答案 1 :(得分:0)
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()
在模板中,删除“()”,我相信你知道如何在模板中使用它。