print default value wtforms in jinja2 - python

I'm using WTforms in Flask with Jinja2. I create a form like this:
def print_form(request, db):
class F(Form):
name = StringField('name', default='Mike')
video = SelectField(
'Programming Language',
choices=[('cpp', 'C++'), ('py', 'Python'), ('text', 'Plain Text')],default='py'
)
form1 = F(request.form)
data = {'form1' : form1}
rtemplate = jinja_env2.get_template('test.html')
data = rtemplate.render(**data)
Then I print the form in Jinja2 like this:
<table>
{% for field in form1 %}
<tr>
{% if field.type == "BooleanField" %}
<td></td>
<td>{{ field }} {{ field.label }}</td>
{% elif field.type == "HiddenField" %}
<td></td>
<td>{{ field }}</td>
{% elif field.type == "SelectField" %}
<td>{{ field.label }}</td>
<td>{{ field }}</td>
{% else %}
<td>{{ field.label }}</td>
<td>{{ field }}</td>
{% endif %}
</tr>
{% endfor %}
</table>
However I cannot find how to prefill the default value of the TextField, it's works automatically with the SelectField.

Related

Iterating over a list from an object in Django template

I am trying to iterate over a list that is below an object in a Django template.
Unfortunately I am not getting anywhere.
This is the object:
{'Value1': [8, 5, 4, 7, 4, 5], 'Value2': [], 'Value3': []}
I am doing in the template something like:
{% for entry in data %}
<tr>
<td>{{ entry }}</td>
<td>{{ entry.0 }}</td>
</tr>
{% endfor %}
or
{% for entry in data %}
<tr>
<td>{{ entry }}</td>
{% for i in entry %}
<td>{{ i }}</td>
{% endfor %}
</tr>
{% endfor %}
But all I am getting is just the first letter of the key.
You have to look for both - keys and values in dict's items:
{% for key, value_list in data.items %}
<tr>
<td>{{ entry }}</td>
{% for value in value_list %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
Did you try iterating over the items in your dict, like so?
{% for key, value in data.items %}

Django Listview - Filter and Pagination

I'm new to django. I'm trying to create two table, first one with the lastest added products filter by publication_date and second one with the upcoming products filter by release_date.
Here my homepage.html code :
...
{% for prodotto in products %}
<tr>
<td>{{ prodotto.title }}</td>
<td>{{ prodotto.description }}</td>
<td>{{ prodotto.publication_date|date:"d/m/Y" }}</td>
<td>{{ prodotto.release_date|date:"d/m/Y" }}</td>
</tr>
{% endfor %}
...
...
{% for prodotto in products %}
<tr>
<td>{{ prodotto.title }}</td>
<td>{{ prodotto.description }}</td>
<td>{{ prodotto.publication_date|date:"d/m/Y" }}</td>
<td>{{ prodotto.release_date|date:"d/m/Y" }}</td>
</tr>
{% endfor %}
here my model.py :
class Products(models.Model):
...
title = models.CharField(max_length=100)
publication_date = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=1000)
slug = models.SlugField(unique=True)
release_date = models.DateTimeField()
...
here my view.py :
class home(ListView):
queryset = Products.objects.all()
template_name = 'homepage.html'
context_object_name = "products"
Currently i'm able to only show all the products without filtering.
I'm trying to also add pagination only in the first table,but I don't have an idea how to filter and add pagination in my homepage.html.
I've tried adding pagination by adding paginate_by = 5 into view.py but with this method,it's adding pagination in both tables.
You can do this directly in your template:
{% for prodotto in products|slice:":5" %}
<tr>
<td>{{ prodotto.title }}</td>
<td>{{ prodotto.description }}</td>
<td>{{ prodotto.publication_date|date:"d/m/Y" }}</td>
<td>{{ prodotto.release_date|date:"d/m/Y" }}</td>
</tr>
{% endfor %}

Creating multiple annotations using a for loop

Here is my code:
phones = Customer.objects.filter(active=True).values('name')\
.annotate(count = Count('phone',filter=Q(phone__model__icontains=model_list[0]))
.annotate(count1 = Count('phone',filter=Q(phone__model__icontains=model_list[1]))
.annotate(count2 = Count('phone',filter=Q(phone__model__icontains=model_list[2]))
.annotate(count3 = Count('phone',filter=Q(phone__model__icontains=model_list[3]))
.annotate(count4 = Count('phone',filter=Q(phone__model__icontains=model_list[4]))
........
html
{% if phones %}
{% for phone in phones %}
<tr>
<td>{{ phone.name }}</td>
<td>{{ phone.count }}</td>
<td>{{ phone.count1 }}</td>
<td>{{ phone.count2 }}</td>
<td>{{ phone.count3 }}</td>
<td>{{ phone.count4 }}</td>
</tr>
{% endfor %}
{% enfif %}
My model_list still has many models. What should I do to simplify these using for loop?
If my model_list has 100 models, this will be very complicated.
I've tried this:
for i in range(len(model_list)):
phone= Customer.objects.filter(active=True).values('name')\
.annotate(count = Count('phone',filter=Q(phone__model__icontains=model_list[i]))
html
{% if phones %}
{% for phone in phones %}
<tr>
<td>{{ phone.name }}</td>
<td>{{ phone.count }}</td>
</tr>
{% endfor %}
{% endif %}
But the result is not what I want, because I only get one of the data.
For example :model_list[0]
Do like this to query for counts:
phones = Customer.objects.filter(active=True).values('name')
for idx, model in enumerate(model_list):
counts = {'count%s' % idx : Count('phone',filter=Q(phone__model__icontains=model)}
phones = phones.annotate(**counts)
Then you would need to pass a range of models to the template (say in models_idx_range as context parameter models_idx_range=range(models_count)) and iterate over counts:
{% if phones %}
{% for phone in phones %}
<tr>
<td>{{ phone.name }}</td>
{% for idx in models_idx_range %}
<td>{{ getattr(phone, 'count%s' % idx) }}</td>
</tr>
{% endfor %}
{% endif %}
I'm not a django expert but i think this is your error:
phone = Customer.objects.filter(active=True).values('name')
for i in range(len(model_list)):
phone= phone.annotate(count = Count('phone',filter=Q(phone__model__icontains=model_list[i]))
You always override your phone with the last value from the for-loop
Try this one
phones = Customer.objects.filter(active=True).values('name')\
.annotate(count = Count('phone',filter=Q(phone__model__icontains=[i for i in model_list]))

How to go through two loops in django templates

Can anyone tell me what am I doing wrong in this code:
{% for dayName in data %}
<tr>
<td>{{ dayName }}</td>
{% for value in data.dayName %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
data is an object containing arrays, for an instance:
data['Sunday'] = [1 ,2 ,3]
And all what I want to do is create two loops through that object.
I will be thankful for each form of help,
Thanks in advance
dayName is a variable not the key itself. data.dayName is interpreted as data['dayName'], that's why you're not getting the right results.
Instead, you can do:
{% for dayName, vals in data.items %}
<tr>
<td>{{ dayName }}</td>
{% for value in vals %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}

Python-Django: user info in template

I have database, currently from my database its displaying like this in my Template(HTML table) page.
sno--scene--frames--empID
1------001----24-----100
2------002----34-----100
3------003----20-----101
4------004----15-----101
5------005----10-----100
But i want to display like this(below). How to get this from HTML(tables). I am using Python-Django.
sno---scene---frames---empID
1--------001-----24-------100
2--------002-----34-------100
3--------005-----10-------100
------------- tot=68
1------003-----20--------101
2------004-----15--------101
-------------tot=35
You should prepare the data in a view - group them by id, calculate sum. Result cal looks like this:
items_info = [{'items':[item1, item2, item3], 'total': 68}, {'items':[item4, item5], 'total': 35}]
Then template should be like this:
{% for info in items_info %}
{% for item in info.items %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ item.scene }}</td>
<td>{{ item.frames }}</td>
<td>{{ item.id }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="3">TOTAL</td>
<td>{{ info.total }}
</tr>
{% endfor %}
Hope this helps!

Categories

Resources