Generating random ID from list - jinja

2024/9/26 5:22:01

I am trying to generate a random ID from a list of contacts (in Python, with jinja2) to display in an HTML template.

So I have a list of contacts, and for the moment I display all of them in a few cells in my HTML template by looping through the list of contacts:

# for contact_db in contact_dbs<tr><td>{{contact_db.key.id()}}</td><td>{{contact_db.name}}</td><td>{{contact_db.phone}}</td><td>{{contact_db.email}}</td></tr># endfor

The view that renders the above is:

def contact_list():contact_dbs, contact_cursor = model.Contact.get_dbs(user_key=auth.current_user_key(),)return flask.render_template('contact_list.html',html_class='contact-list',title='Contacts',contact_dbs=contact_dbs,next_url=util.generate_next_url(contact_cursor),)

Instead, I would like to display one contact, selected randomly by its ID, and it should display another contact with all its information every time the user refreshes the page (I am not dealing with displaying the same contact twice for now by the way).

I know that it is possible to use random in a python file to deal with random choices, so but not sure how it translates in jinja in the template.

Any help appreciated thanks!

Answer

There is a random filter in jinja2.

random(seq)

Return a random item from the sequence.

Use it like this:

{% set selected_contact = contact_dbs|random %}

note: I assumed contact_dbs is iterable.

https://en.xdnf.cn/q/71495.html

Related Q&A

Unit testing Flask app running under uwsgi

I’m relatively new to python and am looking for a pythonic way to handle this practice. I’ve inherited a fairly trivial Python 2.7 Flask app that runs under uwsgi that I want to add some unit tests t…

fastest way to find the smallest positive real root of quartic polynomial 4 degree in python

[What I want] is to find the only one smallest positive real root of quartic function ax^4 + bx^3 + cx^2 + dx + e [Existing Method] My equation is for collision prediction, the maximum degree is quarti…

Split strings by 2nd space

Input :"The boy is running on the train"Output expected:["The boy", "boy is", "is running", "running on", "on the", "the train"]Wha…

Searching for a random python program generator

Im searching for a program that can generate random but valid python programs, similar to theRandom C program generator. I was trying to do this myself giving random input to the python tokenize.untoke…

Python tk framework

I have python code that generates the following error:objc[36554]: Class TKApplication is implemented in both /Library/Frameworks/Tk.framework/Versions/8.5/Tk and /System/Library/Frameworks/Tk.framewor…

SQLAlchemy relationship on many-to-many association table

I am trying to build a relationship to another many-to-many relationship, the code looks like this: from sqlalchemy import Column, Integer, ForeignKey, Table, ForeignKeyConstraint, create_engine from …

Python: interpolating in a triangular mesh

Is there any decent Pythonic way to interpolate in a triangular mesh, or would I need to implement that myself? That is to say, given a (X,Y) point well call P, and a mesh (vertices at (X,Y) with val…

Customizing pytest junitxml failure reports

I am trying to introspect test failures and include additional data into the junit xml test report. Specifically, this is a suite of functional tests on an external product, and I want to include the p…

python nltk keyword extraction from sentence

"First thing we do, lets kill all the lawyers." - William ShakespeareGiven the quote above, I would like to pull out "kill" and "lawyers" as the two prominent keywords to …

Getting the parameter names of scipy.stats distributions

I am writing a script to find the best-fitting distribution over a dataset using scipy.stats. I first have a list of distribution names, over which I iterate:dists = [alpha, anglit, arcsine, beta, bet…