I have a problem. Consider an HTML table and it has rows and columns. I want to add a new row or column when I want it and I want it to be a record in the database. And I want to do this with django. What should I do?
I think I need to use django_table2 but I don't know how. I would be glad if you write a code sample. Thank you)
Say you have a model, you could get a list of objects like so;
def my_view(request):
context = {'object_list':MyModel.objects.all()}
return render(request, 'mypage.html', context)
Then in the template, you could do a few things to create tables:
Either, fully generate the table with Django, like so:
{% for object in object_list %}
<tr>
<td>{{object.data}}</td>
<td>{{object.data}}</td>
<td>{{object.data}}</td>
</tr>
{% endfor %}
This would create a new row for every object.
Another solution is:
{% for object in object_list %}
//Create row for every object
<tr>
{% for data in object.get_field_data %}
// Create column for every field in object
<td>{{data}}</td>
{% endfor %}
</tr>
{% endfor %}
Where get_field_data would be defined as a method on the model like so:
def get_field_data(self):
datalist = []
for field in self._meta.get_fields():
datalist.append(getattr(self, field.name))
return datalist
You could then even implement some checks on the get_field_data, for example, you could exclude fields.
def get_field_data(self):
datalist = []
for field in self._meta.get_fields():
if field.name != 'id':
datalist.append(getattr(self, field.name))
return datalist
You don't need to use any external package, you can do this easily by using the Django Template Language
Here is a code example using a ListView
# views.py
from django.views.generic import ListView
class ItemListView(ListView):
template_name = 'mytable.html'
model = MyModel
context_object_name = 'item_list'
<!-- mytable.html -->
...
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
{% for item in item_list %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.amount }}</td>
</tr>
{% endfor %}
</tbody>
</table>
...
In this way your template will have access to a list of objects called item_list which corresponds exactly to the records in the database table. You can cycle that list/queryset using a for loop that will automatically wrap that content in the html needed to make it part of the table.
Related
I have queries like following that I would like to implement it in front end:
MembershipPayment.objects.filter(group__name=tg.group_name).are_valid().count()
MembershipPayment.objects.filter(group__name=tg.group_name).Not_valid().count()
I know that I can pass this from view to front-end HTML, but the problem is that in the front end I have a query-set containing many "group"s. so I need to run a similar query for each of those "group"s.
I need something along these lines [this code of course won't work] in front:
{% for rec in groups %}
<tr>
<td>{{ MembershipPayment.objects.filter(group__name=tg.group_name).are_valid.count }}</td>
<td>{{ MembershipPayment.objects.filter(group__name=tg.group_name).not_valid.count }}</td>
</tr>
{% endfor %}
So I was wondering how can I achieve this without changing my model structure (if possible).
Why don't you do that logic in your view, by building a dict for each of your group, like:
def your_view(request):
...
groups: dict = dict()
for group_name in group_names:
groups[group_name]: int = MembershipPayment.objects.filter(group__name=group_name).are_valid().count()
return render(request,'your_template.html', {'groups': groups})
..and then pass it to your template, like:
{% for key, value in groups.items %}
<tr>
<td>{{key}}</td>
<td>{{value}}</td>
</tr>
{% endfor %}
...key being the group name, value being the group count.
I ended up passing a dictionary like this in view:
groups_statistics={group1:{monthly_balance:250,weekly_balance:3000},group2:{monthly_balance:250,weekly_balance:3000}}
and then looping through its value as:
{% for rec,stat_dic in groups_statistics.items %}
<tr>
<td>{{ stat_dic.weekly_balance| get_value_from_dict:rec }}</td>
<td>{{ stat_dic.monthly_balance| get_value_from_dict:rec }}</td>
</tr>
{% endfor %}
here get_value_from_dict is a customized tag defined in dictionarytags.py as:
from django import template
register = template.Library()
#register.filter('get_value_from_dict')
def get_value_from_dict(dict_data, key):
"""
usage example {{ your_dict|get_value_from_dict:your_key }}
"""
if key:
return dict_data.get(key)
in the following folder:
my app>"templatetag" folder.
this folder contains:
__ init __.py
and
dictionarytags.py
and then in html I have this line in upper part:
{% load dictionarytags %}
I have two Django models that record time. Model one records time during the morning and Model two records time during the evening. I want to present both of these times along with the difference between the times within an HTML table but am confused about how to do it. I am new to Django and would really appreciate some advice.
This is what I have so far:
models.py:
class Alltime(models.Model):
id= models.ForeignKey(User, on_delete = models.CASCADE)
mtime = models.DateTimeField()
etime = models.DateTimeField()
views.py:
def panel(request):
time_data = User.objects.filter(pk__gt=1) #I need all data except for the default Super User account
get_time = Alltime.objects.all()
return render(request, 'users/interface.html', {'data': time_data, "get_time": get_time})
panel.html:
<form>
{% csrf_token %}
<table>
<tr>
<th>Name</th>
<th>Morning timeE</th>
<th>Evening time</th>
<th>Difference in hours</th>
</tr>
{% for data in data %}
<tr>
<td>{{data.username}}</td>
{% endfor %}
{% if get_time %}
{% for m in get_time %}
<td>{{m.mtime}}</td>
<td>{{m.etime}}</td>
{% endfor %}
{% else %}
<td> Not available </td>
{% endif %}
</tr>
</table>
</form>
How can I get the difference between the times and place them within the HTML table?
If I understand correctly what you want to do, then you can/need to structure your data differently. An easy way is to prepare the data in your view:
def panel(request):
time_data = User.objects.filter(pk__gt=1)
time_table=[]
for user in time_data:
morning_time = Morning.objects.filter(user=user)
evening_time = Evening.objects.filter(user=user)
diff = morning_time - evening_time
time_table.append((user.name, morning_time, evening_time, diff))
return render(request, 'users/interface.html', {'data': time_table})
And in the template:
<table>
<tr>
<th>Name</th>
<th>Morning timeE</th>
<th>Evening time</th>
<th>Difference in hours</th>
</tr>
{% for line in data %}
<tr>
<td>{{line.0}}</td>
<td>{{line.1}}</td>
<td>{{line.2}}</td>
<td>{{line.3}}</td>
</tr>
{% endfor %}
</table>
You need to add the handling of not existing data in the view code.
Some remarks:
The whole thing does not really make sense to me. I guess you will need to filter for dates too. But you should get the idea from this. And why is it in a form?
You can add a property to the Alltime model that returns the difference between the morning and evening time
#property
def diff(self):
return self.etime - self.mtime
Then in your template you can use this property
{% for m in get_time %}
<td>{{m.mtime}}</td>
<td>{{m.etime}}</td>
<td>{{m.diff}}</td>
{% endfor %}
I am trying to split a list from my model across two columns, using this html code in the template:
< div class ="col-md-6" >
{%for value in object_list %}
<ul>< ahref="/sites/{{value.url}}/">{{value.Site}}</a></ul>
{% endfor %}
I was planning to achieve this with the slice tag to filter the list, e.g.:
{%for value in object_list|slice:"10:20" %}
It does not work however, and I think it might be because I have context data i.e. {{value.Site}}, instead of just {{Site}} for example. This is the corresponding view:
class homeview(ListView):
template_name = 'annual_means/home.html'
def get_queryset(self):
return AnnualMean.objects.values("Site", "url").distinct()
What do I need to do to get the slice to work?
I think, what you need is this:
<table>
<tr>
<th>URL</th>
<th>SITE</th>
</tr>
{% for value in object_list %}
<tr>
<td>{{value.url}}</td>
<td>{{value.Site}}</td>
</tr>
{% endfor %}
</table>
URLs and Sites will be displayed as a table.
I'm trying to display the model data in the template.
When I don't have any filtering, it works fine!
class UserLogList(ListView):
context_object_name = 'data'
queryset = UserLog.objects.all().values('user_id__username','event_id','time')
But if I want to have some filtering, example if I want to get the details based on the user_id where user_id = 1. I can get the user id from the request object or self.request object.
So to achieve this, I did the following in my views.py
class DisplayUserActivity(ListView):
template_name = 'user_activity.html'
uid = self.request.user.id
def get_object(self):
uid = self.request.user.id
object = super(DisplayUserActivity,self).get_object()
object.data = UserLog.objects.filter(user_id = uid).values('user_id__username','event_id','time')
return object
and my template file:
{% block content %}
<h2> Logs </h2>
<table border="1" style="width:50%">
<tr>
<th> User Name </th>
<th> Description </th>
<th> Time </th>
</tr>
{% for user in data %}
<tr>
<td> {{user.user_id__username}}</td>
<td> {{user.event_id}}</td>
<td> {{user.time}} </td>
{% endfor %}
</tr>
</table>
{% endblock %}
What am I missing?
I think you need a method called get_queryset instead of get_object when extending the generic ListView.
EDIT:
Regarding your edit, theres an issue with your template
{% for user in data %}
should be
{% for user in object_list %}
But I suspect there are deeper problems here which are hard to decipher. My advice is to look through your code and the Django documentation more carefully, and possibly switch to a custom written view until you are more comfortable. :)
I am working on a dynamic template and i hoped to be able to include the last part of it aswell. My problem: I got a x-amount of fields that relate to fields of the object. But when i pass another model object it should show other fields.
Example:
model 1 show object name, category, approved
model 2 show object name, attribute,
For the other variable things, i make a dictionary that holds the information, problem is, i dont know how i could link the object fields to it.
Dictonary send to template:
field_map = {'field1': 'Name', 'field2': 'Category', 'field3': 'Approved'}
Template:
{% if objects|length > 0 %}
<table style="padding: 3px;">
<tr style=" text-align: center;">
{% for key, value in field_map %}
<th>{{ value }}</th>
{% endfor %}
</tr>
{% for object in objects %}
<tr>
<td>{{ object.name }}</td>
<td>{{ object.category }}</td>
<td>{{ object.approved }}</td>
</tr>
But now i want to add the objects fields to the field_map fields.
Cause i will be sending more then 1 object
Hope i explained my question well enough.
Can someone help me out?
You are trying to re-implmenet an already solved problem. Please use django-tables2 to render tables in django: https://github.com/bradleyayers/django-tables2
Update: To answer OP's comment on how to remove or edit the pagination footer:
If you do not want to have pagination to your table then just configure your table passing paginate=None to your RequestConfig like this:
table = TestTable(Test.objects.all())
RequestConfig(request, paginate=None).configure(table)
return render(request, 'test.html', {'table': table})
If you want to edit the footer yourself then you have to provide a new table rendering template. To do this, you just need to copy the contents of table.html from here https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html to your templates directory. You can then modify this file to your requirements, for instance the pagination section is between {% if table.page %} and the {% endif %} at the end of the file.
Now, after you've created your custom table template you need render your table using this template. If you named your template as mytable.html then just use the following syntax from within your normal templates:
{% render_table mytable "mytable.html" %}