Reputation: 79
I have encountered a problem when i wanted to display some information from my list and some other information from my object. Here is what i have done so far.
I have this two class here :
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")
Here is my view
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)
and here is my html page: # i am stuck here !
{% 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>
i want to show some real data instead of showing "here is some text"
by using the Document
Class i have shown above so i can use {{document.title}}
and {{document.description}}
but i can't find a way to do so
any help will be appreciated
Upvotes: 1
Views: 788
Reputation: 1442
it's just a simple query as Daniel mentioned above notice that you have a foreign key:
documents = Document.objects.all().filter(course_name__courses__iexact=file_name)
context["documents"] = documents
Upvotes: 2