Reputation: 75
In view.py:
class DisplayView(generic.ListView):
def get(self, request, **kwargs):
if request.method == "GET":
selection = request.GET.getlist("selection") # list of checkbox values with name 'selection
articles = Article.objects.all()
args = {'articles': articles, 'selection': selection}
print(selection)
print(articles)
return render(request, 'display.html', args)
In display.html:
{% extends 'myapp/base.html' %}
{%block contentblock %}
{% for section in selection %}
<h1>{{ section }}</h1>
<script language="javascript">console.log(selection)</script>
{% endfor %}
{% endblock %}
I can see on my terminal 'selection' and 'articles' printed, meaning that the view correctly got the data from the checkboxes. However, nothing appears in my html and the console.log throws an error meaning the data was not passed to the template... Why?
Upvotes: 1
Views: 63
Reputation: 27513
change this line
return render(request, display.html, args)
to
return render(request, 'display.html', args)
mind the html is right inside templates and not inside any other subfolder, if subfolder is there add the folder name before it inside single quotes
and in template
{% extends 'myapp/base.html' %}
{% block contentblock %}
<script language="javascript">console.log({{ selection }})</script>
{% for section in selection %}
<h1>{{ section }}</h1>
<script language="javascript">console.log({{ articles }})</script>
{% for article in articles %}
<h2>{{ article }}<h2>
{% endfor %}
{% endfor %}
{% endblock %}
Upvotes: 0
Reputation:
for script you need add brackets:
<script language="javascript">console.log({{ selection }})</script>
Upvotes: 3
Reputation: 504
First, you should add {% endfor %} for section in selection as well, you are using two for loop. Second, you need to add '' to template name in return render
Upvotes: 0