Bryce
Bryce

Reputation: 321

Passing Dictionary from Views to Template

First off, I have looked at many other sources to try and get help, but I feel like I basically have what they have and it just is not working for me.

So I have this as my Views:

class MVS(ListView):
    model = VTSI
    template_name = "templates/mvs.html"
    def index(request):
        q = VTSI._meta.get_fields()
        d = {}
        for x in q:
            z = x.verbose_name
            d.update({z:z})
        return render(request, 'mvs', {'d': d})

Simplified, the above class is trying to get all the column names in the models file and get the verbose name of them. It will then take those verbose names, put it in a dictionary, and then pass it to templates to display all those column names. I previously tried passing a list, but I learned that a dictionary is needed to pass values to a template.

This is what I have for my templates:

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load staticfiles %}
{% block content %}
<div class="row">
  <div class="col-sm-3">
    <h2>Description of Table</h2>
  </div>
</div>
<table id="listview" class='table table-striped table-bordered'>
<thead>
<tr>
  <th>Column One</th>
  <th>Column Two</th>
</tr>
</thead>
<tbody>
{% for v in d.items %}
<tr>
<td> {{ v }}</td>
<td> </td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

This template is blank and does not show any of the column names. What am I missing from this and how can I fix this?

Upvotes: 0

Views: 1349

Answers (2)

Bryce
Bryce

Reputation: 321

So I found the answer to my own question after help from user6731765 above, what I did was this:

class MVS(ListView):
model = VTSI
template_name = "templates/mvs.html"    
def get_context_data(self, **kwargs):
    context = super(MVS, self).get_context_data(**kwargs)
    context['v'] = VTSI._meta.get_fields()        
    return context

And then in my templates, I did this:

{% for a in v %}
<tr>
  <td> {{ a.verbose_name }}</td>
  {% empty %} 
    <td> Sorry, empty </td>
  <td> </td>
</tr>
{% endfor %}

Edit: For future readers, the main point in all of this is the dictionary_value['something_here'] part. For some reason I can't exactly explain, you need to have that added to the dictionary variable, and then you'll iterate over that 'something_here' part in the template. You can go and do anything you want in other methods and then call it by just doing:

dictionary_variable['something_here'] = self.some_other_method()
return dictionary_variable

In the templates you would iterate over that 'something_here' part again to display whatever.

Upvotes: 0

NS0
NS0

Reputation: 6096

Have a look at the documentation for using ListView

Try changing your code to look more like

class MVS(ListView):
    model = VTSI
    template_name = "templates/mvs.html"
    def get_context_data(self, **kwargs):
        q = VTSI._meta.get_fields()
        d = {}
        for x in q:
            z = x.verbose_name
            d.update({z:z})
        return d

Upvotes: 0

Related Questions