当我想显示列表中的某些信息和对象中的其他信息时,我遇到了问题。这是我到目前为止所做的。
我在这里有这两节课:
class CoursesNames(models.Model):
DB = "data structure"
SF = "software enginering"
courseChoices = (
(DB, "data structure"),
(SF, "software enginering"),
)
courses = models.CharField(max_length=50, choices=courseChoices)
class Document(models.Model):
course_name = models.ForeignKey(
CoursesNames, on_delete=models.CASCADE, related_name='document_courses')
title = models.CharField(max_length=100)
description = models.TextField(help_text="some text for now")
这是我的观点
def showDocuments(request, *args, **kwargs):
documents = Document.objects.all()
context = {
"documents": documents,
"files": [], # an empty list for now
}
# After writing some logic
context["files"].append(item)
file_name = request.POST["course"]
context["filename"] = file_name
return render(request, 'dashboard.html', context)
这是我的html页面:#我被困在这里!
{% if files %}
<div class="container">
<div class="row">
{% for file in files%}
{% for document in documents %}
{% if document.course_name == filename %}
<div class="col-sm-4 mt-4">
<div class="card">
<div class="card-header ">
{{filename}}
</div>
<div class="card-body">
<h5 class="card-title">{{document.title }}</h5>
<p class="card-text">this is a text where you should the best compilation of files that will definitely will help you get a st
straight a inshallah</p>
<a href="http://127.0.0.1:8000/media/documents/{{filename}}/{{file}}" class="btn btn-success" download>Download</a>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
</div>
我想显示一些真实数据,而不是使用上面显示的"here is some text"
类来显示Document
,因此我可以使用{{document.title}}
和{{document.description}}
,但我可以找不到办法
任何帮助将不胜感激
答案 0 :(得分:0)
这只是Daniel上面提到的一个简单查询 请注意,您有一个外键:
documents = Document.objects.all().filter(course_name__courses__iexact=file_name)
context["documents"] = documents