【发布时间】: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