Skip to content

Instantly share code, notes, and snippets.

@hvgab
Created March 10, 2019 22:49
Show Gist options
  • Save hvgab/1af2770367bd10c54c7392c766001c89 to your computer and use it in GitHub Desktop.
Save hvgab/1af2770367bd10c54c7392c766001c89 to your computer and use it in GitHub Desktop.
Dynamic ListView for Django while developing/making proof of concept
{% extends 'base.html' %}
{% load my_extras %}
{% block content %}
<section class="section container">
<table class="table">
<thead>
<tr>
{% for field in fields %}
<td>{{ field }}</td>
{% endfor %}
</tr>
</thead>
{% for row in object_list %}
<tr>
{% for field in fields %}
<td>{{ row|get_attr:field }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</section>
{% endblock %}
from django import template
register = template.Library()
@register.filter
def get_attr(value, arg):
try:
return getattr(value, arg)
except:
return
# GAME
class GameListView(ListView):
model = Game
template_name = 'bgb_app/generic_list.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['fields'] = [
field.name for field in self.model._meta.get_fields()
]
return context
@Bastilla123
Copy link

Very cool Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment