Reputation: 3149
I'm making a simple blog for myself in Django, but having a strange issue. I have a simple model as shown below:
Entry:
- title
- content
This is the template I'm currently using:
{% for entry in context.entries %}
<div class="row">
<div class="col-xl-2 col-lg-2 col-md-2 hidden-sm hidden-xs"></div>
<div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12">
<div class="entry">
<div class="header">
<table>
<tr>
<td class="title"><h2>{{ entry|title }}</h2></td>
<td class="datetime">01.01.2016 00:00:00</td>
</tr>
</table>
</div>
<div class="content">
{{ entry|content }}
</div>
<div class="footer">
<a href="#">#some</a>
<a href="#">#tag</a>
<a href="#">#right</a>
<a href="#">#here</a>
</div>
</div>
<div class="col-xl-2 col-lg-2 col-md-2 hidden-sm hidden-xs"></div>
</div>
</div>
{% endfor %}
However, I'm getting TemplateSyntaxError on the {{ entry|content }}
line, which is strange.
views.py file:
import json, termcolor
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
def index(request, page="1"):
page = int(page)
context = {
"title": "Erdin's Blog",
"entries": []
}
entries = Entry.objects.all()
r_entries = entries[::-1]
del entries
interest = [page*10-10, page*10]
context["entries"].extend(r_entries[interest[0]:interest[1]])
print(termcolor.colored(context["entries"][0].content, "green"))
return render(request, "home.elms.html", context)
I already have a single entry on database. I also checked if it's queried correctly into database with sqliteman
. Assuming I named this single entry as entry
variable, I called entry.id
, entry.title
and entry.content
and printed out to terminal, that was successful. There's no problem on database.
I currently realized the problem was completely different. I called {% for entry in context.entries %}
but I already called context into template, so the engine was looking for a context
key inside context
dictionary.
Upvotes: 0
Views: 96
Reputation: 3149
Since I solved my own problem, I'm writing here for the sake of other users having same problem.
Since I returned a render in views.py as below:
render(request, "home.elms.html", context)
The template should call for dictionary inside the context as:
{% for entry in entries %}
not:
{% for entry in context.entries %}
Upvotes: 0
Reputation: 473753
The |
syntax is for template filters. To get the model values in the template there is a dot notation:
{{ entry.title }}
{{ entry.content }}
Note that the reason why {{ entry|title }}
did not throw any errors is that there is a built-in title
template filter. But there is no content
template filter - this is why you see the error on the line containing {{ entry|content }}
.
Upvotes: 1