I have a Django table with 3 fields.
I have a total of 4 entries and i want to visualize all of them inside a table which only displays 2 of these 3 fields.
How should i do that? i did manage to only display 1 row instead of 4, i'm struggling, someone could help?
This is the easy model to manage.
class Inc(models.Model):
id = models.AutoField(primary_key=True)
nr_sin = models.CharField(max_length=50, default=None)
nr_pol = models.CharField(max_length=50, default=None)
Have you already got the QuerySet being passed from your view to your templates?
Assuming you have, and it's in a variable called inc_list, then in your template you could do this to display the nr_sin and nr_pol fields:
{% if inc_list %}
<table>
<thead>
<tr>
<th>NR SIN</th>
<th>NR POL</th>
</tr>
</thead>
<tbody>
{% for inc in inc_list %}
<tr>
<td>{{ inc.nr_sin }}</td>
<td>{{ inc.nr_pol }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
In you view query your model to get all the entries you want to display in the table and pass them as a context variable to the templete. Later in the template you have to iterate over the list of elements and render one by one as follow:
# import your model
class SomeView(View):
def get(self, request):
elements = Inc.objects.all()
context = {'elements': elements}
return render(request, 'your_template.html', context)
render the table:
<table>
<thead>
<tr>
<th>ID</th>
<th>nr_sin</th>
</tr>
</thead>
<tbody>
{# you loop over the list and render one element at each iteration #}
{% for inc in elements %}
<tr>
<td>{{ inc.pk }}</td>
<td>{{ inc.nr_sin }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Related
I'm trying to render results of a group by query from a view to an HTML table but it is returning nothing. I originally had success in listing all results, but after applying the sum aggregation I can't get it to appear.
Django View - with Group By
def get_asset_price(request):
# CryptoAssets is the model
obj = CryptoAssets.objects.filter(user_id=request.user.id).values('symbol')
obj = obj.annotate(total_units=Sum('units'),
total_cost=Sum('cost'))\
.order_by('symbol')
# Returns list of dictionaries
context = {
'object': obj
}
return render(request, 'coinprices/my-dashboard.html', context)
HTML
<style>
table, th, td {
border: 1px solid black;
}
</style>
<div class="container">
<h1>My Dashboard</h1>
<table>
<tr>
<th>Symbol</th>
<th>Total Units</th>
<th>Total Cost</th>
</tr>
{% for row in object %}
<tr>
<td>{{ row.symbol }}</td>
<td>{{ row.units }}</td>
<td>{{ row.cost }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
I'll provide the view below that worked without the group by.
Django View - No group by
def get_asset_price(request):
# CryptoAssets is the model
obj = CryptoAssets.objects.all().filter(user_id=request.user.id)
# Returns list of objects
context = {
'object': obj
}
return render(request, 'coinprices/my-dashboard.html', context)
I think you're calling an attribute which does not exist on your object. The type of your object is a list of dictionaries. Change your template codes to something like this (this is the simplest way and maybe you can improve it later like adding a template tag similar to the one that is used in this question):
<table>
<tr>
<th>Symbol</th>
<th>Total Units</th>
<th>Total Cost</th>
</tr>
{% for row in object %}
<tr>
{% for key, value in row.items %}
<td>{{ key }}</td>
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Does anybody knows how can I use count based on selected value using django_filters
Error
'UserFilter' object has no attribute 'count'
My Reference link
views.py
def search(request):
user_list = Person.objects.all()
user_filter = UserFilter(request.GET, queryset=user_list)
count = user_filter.count() #this will return an error
print(count)
return render(request, 'user_list.html', {'filter': user_filter})
filters.py
from django.contrib.auth.models import User
from .models import Person
import django_filters
class UserFilter(django_filters.FilterSet):
class Meta:
model = Person
fields = ['category', 'firstname', 'lastname' ]
user_list.html
{% extends 'base.html' %}
{% block content %}
<form method="get">
{{filter.form.as_p}}
<button type="submit" >Search</button>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>Firstname</th>
<th> Lastname</th>
<th>Caegory</th>
</tr>
</thead>
<tbody>
{% for user in filter.qs %}
<tr>
<td>{{ user.firstname }}</td>
<td>{{ user.lastname }}</td>
<td>{{ user.category }}</td>
</tr>
{% empty %}
<tr>
<td colspan="5">No data</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
I want to count all the list base from data I filtered
You'll want the count of the resulting queryset, which you can get from the filter's qs property (as you do in your template!).
Change
count = user_filter.count()
to
count = user_filter.qs.count()
You can work with the .qs attribute:
def search(request):
user_list = Person.objects.all()
user_filter = UserFilter(request.GET, queryset=user_list)
count = user_filter.qs.count()
return render(request, 'user_list.html', {'filter': user_filter})
.qs [GitHub] is a property that generates a QuerySet by filtering the original queryset by values in the fields of the FilterSet.
My Django View:
def hub(request):
context = {}
hub_id = [value['id'] for value in hub_data['data']]
hub_name = [value['attributes']['name'] for value in hub_data['data']]
hub_url = [value['links']['self']['href'] for value in hub_data['data']]
nested_dict = dict(zip(hub_name, map(list, zip(hub_id, hub_url))))
context ['rows'] = nested_dict
return render(request, 'connector/hub.html', context)
The context['rows'] results in:
{'rows': {hub_name1 : ['hub_id1', 'hub_url1'],{hub_name2 : ['hub_id2', 'hub_url2'], etc.. }
I am trying to pass it a HTML table that looks like this:
<th scope="col">Hub Name</th>
<th scope="col">Hub ID</th>
<th scope="col">Hub URL</th>
My tablebody looks like this:
<tbody>
{% for key, value in rows.items %}
<tr>
<td> {{ key }}</td>
<td> {{ value }}</td> **//how do i just get hub_id here**
<td> Don't know what to do here to get: hub_url </td>
</tr>
{% endfor %}
</tbody>
But I want to add another -tag to fill with hub_url. How do I extract the hub_id data and add it to the column: Hub ID and extract the hub_url and add it to the column Hub URL.
Any help would be much appreciated!
You can pass the data to the template without transforming it
return render(request, 'connector/hub.html', {'data': hub_data['data']})
And then lookup the attributes using the "dot" template syntax for each row
<tbody>
{% for row in data %}
<tr>
<td>{{ row.attributes.name }}</td>
<td>{{ row.id }}</td>
<td>{{ row.links.self.href }}</td>
</tr>
{% endfor %}
</tbody>
I'm trying to display a product, and available brands(has product as ForeignKey) for that product through DetailView. Based on Django documentation and similar answers on stackoverflow, I tried below code but it doesn't work. Product details are rendering but names of brands are not. I've checked through django-admin, that the brands the products are present in the database.
Could someone please help.
Models.py
class Product(models.Model):
name = models.CharField(max_length=256)
price = models.IntegerField()
class Brand(models.Model):
name = models.CharField(max_length=256)
product = models.ForeignKey(Product,on_delete=models.PROTECT,related_name='Brands')
Views.py
class ProductDetailView(DetailView):
model = Product
Urls.py
path('detail/<int:pk>/',views.ProductDetailView.as_view(),name='product_detail'),
product_detail.html
<table class="table table-bordered table-hover table-secondary">
<tr>
<th class="bg-secondary th-customer-detail">Name</th>
<td>{{ product.name }}</td>
</tr>
<tr>
<th class="bg-secondary th-customer-detail">Price</th>
<td>{{ product.price }}</td>
</tr>
</table>
<br>
<ul>
{% for brand in product.brand_set.all %}
<li>{{ brand.name }}</li>
{% endfor %}
</ul>
You can do it this way
class ProductDetailView(DetailView):
model = Product
context_object_name = 'product'
def get_context_data(self,**kwargs):
context = super(ProductDetailView,self).get_context_data(**kwargs) #returns a dictionary of context
primary_key = self.kwargs.get('pk')
brands = Brand.objects.filter(product = primary_key)
new_context_objects = {'brands':brands}
context.update(new_context_objects)
return context
<table class="table table-bordered table-hover table-secondary">
<tr>
<th class="bg-secondary th-customer-detail">Name</th>
<td>{{ product.name }}</td>
</tr>
<tr>
<th class="bg-secondary th-customer-detail">Price</th>
<td>{{ product.price }}</td>
</tr>
</table>
<br>
<ul>
{% for brand in brands %}
<li>{{brand.name}}</li>
{% endfor %}
</ul>
How can I get the Field Names to return as text/string?
Highlighted "Field Names" I want returned as text, not fields
I am dynamically creating a list of fields and then appending values. But I can't seem to figure out a way to return the field names as plain text. The below code appends them to a field (fieldname)-- which is the only way I have been able to return them.
class ContractFields(FlaskForm):
fieldname = StringField()
fieldvalue = StringField()
class ContractForm(FlaskForm):
title = StringField('title')
contractfieldlist = FieldList(FormField(ContractFields))
#app.route('/tester.html', methods=['GET','POST'])
def contractfields():
form = ContractForm()
for f in object:
document_form = ContractFields()
document_form.fieldname = f.name #need this list object to return as table text, not a field
document_form.fieldvalue = f.value
form.contractfieldlist.append_entry(document_form)
return render_template('tester.html', form = form)
And from the template:
<div>
<form action="" method="post" name="form">
{{ form.hidden_tag() }}
<div>
<table>
<tr>
<th> ListNumber </th>
<th> Field Name </th>
<th> Field Value </th>
</tr>
{% for items in form.contractfieldlist %}
<tr>
<td>{{ items.label }}</td>
<td>{{ items.fieldname }}</td>
<td>{{ items.fieldvalue }}</td>
</tr>
{% endfor %}
</table>
</div>
<p><input type="submit" name="edit" value="Send"></p>
</form>
</div>
My experience with Python has largely been limited ETL and data transformation so I don't understand why this was so complicated. But after way too many hours I finally found the following solution worked for me.
Specifically modifying the associated excerpt from the above post to be the following:
class ContractFields(FlaskForm):
fieldname = HiddenField()
fieldvalue = StringField()
def __init__(self, *args, **kwargs):
super(ContractFields, self).__init__(*args, **kwargs)
if 'obj' in kwargs and kwargs['obj'] is not None:
self.fieldvalue.label.text = kwargs['obj'].fieldname
And the template html to:
<td>{{ items.label }}</td>
<td>{{ items.fieldvalue.label }}</td>
<td>{{ items.fieldvalue }}</td>