How do I return a value from database to template? - html

I have a database that uses formset to help store data. How do I get the ‘value’ of the numbers of data in the template? For example, I got a queryset of {[Item 1],[ Item 2]}. How do I get the value of 2 in the template to tell me there's 2 items? I want to use this value to control the amount of stuff I can clone with a click of the button. I'm using Django for the web

You just ask |length of the queryset, like:
{{ queryset|length }}
will be returning the number of element

Related

Comparing lists in DBT jinja macro inside for loop

I am storing output of each column in a list for the below query
select table,
columns,
flag
from {{ model }}
for each table I need to compare the flag. how do i get flag in list for the corresponding table
{% for n in table %}
{%- if flag[n] == 'False' %}
I tried as above its not working its returning null value
It's important to understand that jinja is a templating language. In dbt, we use jinja to automate the creation of sql that then gets executed in our data warehouse. Accordingly, data from your data warehouse does not (typically) flow through your jinja code.
flag is a reference to a column in your database, so to evaluate the contents of flag, you need to write sql to do that job.
The only exception is if you are using run_query or a similar macro to pull the data from your database into your jinja context at model-compile time. But I don't see any reference to that in your code (and frankly that's a complicating detail that's only relevant if you are trying to write SQL dynamically). If you are just trying to compare the value from a record in your database as part of your model, just do it in sql.

Input multiple objects of a class in a single HTML or Flask form

For the sake of simplicity, say I want to build a shopping list using Flask using a table in a format that looks like this:
Name
Cost
Amount
Sell-By
Apples
1
5
2022-07-18
Bananas
0.7
7
2022-07-20
I want the shopping list page to initially look like this, with a user able to add any number of items before submission
Name
Cost
Amount
Sell-By
(User input)
(User input)
(User input)
(User input)
When submitted each line should be able to be initiated as a SQLAlchemy object and saved into a database.
I've tried using FieldList and FormField from WTForms but can't quite get the hang of it (and don't really feel it's appropriate in this situation anyway). I don't usually struggle with forms, but having multiple instances of the same form AND trying to put that in a dynamically generated table is really tripping me up, so any pointers are welcome.
The content/size of a shopping list should be adjustable during writing.
Therefore i suggest you take a look at JavaScript and add a simple Button that generates a new row´each time you click it. Each row then contains the needed String or Number fields.
If you send the form you can simply move the entries to your Database

Passing variables with Jinja2 within 3 templates

I have this problem:
I have 3 templates:
Search_user
Show_user
Edit_user
Whit the search_user I'm getting the name value with the post method, then I'll search the data in the db, save the data in an array called user and then pass the array to the Show_user template.
In the Show_user template I show the data with {{ user[0] }}, {{ user[1] }}...{{ user[7] }}, under this data I have a button that bring me to the Edit_user template.
But in the Edit_user template I don't know how to pass the previous data, I don't know hot to export data with the post method or any other methods.
A walk around could be <input type="text" name="surname" value="user[0]" required> but I don't want to show the textbox in the Show_user template.
You don't need to pass any data except some kind of user identifier (a number or a user name) to fetch the user back from the database when needed.
It can be done in multiple ways:
add it to the endpoint URL in the form action (/user/edit/<user-id>)
or use your web framework session to store the user identifier
or add the identifier to your templates as a hidden form field (type=hidden)
...
In any case you just need to get that piece of information (from the URL endpoint, from the session, from the form data...), use it to fetch the user from the database, then pass the user to the edit template.
If you're using a web framework just read the docs, this is a trivial use case that we'll be very likely well documented.
If you need more details, please, share a few code fragments.

Can I allow paging in the contenttype config

I have been playing with the paginator function in Bolt CMS, it is easy to use.
Now I need to know if there is a way to implement the pagination in the contenttype yaml.
I think, is it possible something like this?
entries:
name: Entries
singular_name: Entry
fields:
...
taxonomy: [ categories ]
allowpaging: true
I only have found that you need explicity write the allowpaging flag when you fetch the content via setcontent:
{% setcontent entries = "entries/latest/4" allowpaging %}
But what if you want to use the same template for displaying the related taxonomy records? The problem is that you always will be fetching the last 4 entries regardless the taxonomy.
If there's no way to do this, there would be a way to implementing it?
Paging will also be automatically set if you use listing records setting
listing_records: 10
But your template still needs a pager that will use this setting - the listing templates in the theme/base-2014 will work and can be used as an example
The docs have more information https://docs.bolt.cm/contenttypes-and-records#defining-contenttypes
In config.yml set listing_records: xx or the number of records you want to show
then set in your .twix template {% setcontent entries = "entries/latest/xx" allowpaging %}with the same number
and add at the end of .twix file put this code {{ pager('pages') }} to show pages
You can see the official bolt docs for further informations
https://docs.bolt.cm/3.1/templating/content-paging

Add an aggregation to a django query set

Hopefully easy question. I have a query set of actions and each action has a time_estimate. I want to get the average time_estimate across all of the actions in the queryset such that this returns the average:
{{ actions.avg_time_estimate }}
I tried
actions.avg_time_estimate = actions.aggregate(Avg('time_estimate'))
but this is assigning a dictionary to actions.avg_time_estimate, meaning I get my average like this:
{{ actions.avg_time_estimate.effort__avg }}
What is the correct way to pull the average into the queryset? I also want to retain the actual list of actions in the query.
Try actions = actions.aggregate(avg_time_estimate=Avg('time_estimate'))