Form generation utilities for Piccolo ORM Table class
Project description
Server-side form generation utilities for Piccolo ORM Table class. Based on the code found in wtforms.ext and provides a bridge between Piccolo ORM tables and WTForms.
Installation
pip install wtforms-piccolo
Usage
Example usage:
# table.py
from piccolo.apps.user.tables import BaseUser
from piccolo.columns import Boolean, ForeignKey, Integer, Text, Varchar
from piccolo.table import Table
class Task(Table):
"""
An example table.
"""
name = Varchar(required=True, null=False)
description = Text(required=True, null=False)
views = Integer(required=True, null=False, default=0)
completed = Boolean(default=False)
task_user = ForeignKey(references=BaseUser)
Generate a form based on the table.
TaskForm = table_form(Task)
Generate a form based on the table, excluding 'id' and 'views'.
TaskForm = table_form(Task, exclude=['id', 'views'])
Generate a form based on the table, only including 'name' and 'description'.
TaskForm = table_form(Task, only=['name', 'description'])
The form can be generated setting field arguments:
TaskForm = table_form(Task, only=['name', 'description'], field_args={
'name': {
'label': 'Your new label',
},
'description': {
'label': 'Your new label',
}
})
Example implementation for an edit view using Starlette web app:
# app.py
# other imports
from wtforms_piccolo.orm import table_form
@app.route("/{id:int}/", methods=["GET", "POST"])
async def edit(request):
path_id = request.path_params["id"]
item = await Task.objects().get(Task.id == path_id).run()
users = await BaseUser.select().run()
data = await request.form()
TaskForm = table_form(Task, exclude=["id"])
form = TaskForm(obj=item, formdata=data)
# FK select field
form.task_user.choices = [(i["id"], i["username"]) for i in users]
if request.method == "POST" and form.validate():
form.populate_obj(item)
await item.save().run()
return RedirectResponse(url="/", status_code=302)
return templates.TemplateResponse(
"edit.html",
{
"request": request,
"form": form,
"table_name": Task._meta.tablename,
},
)
Example template for above view using Jinja and Bootstrap:
{% extends "base.html" %}
{% block content %}
<main role="main">
<br><br>
<div class="container">
<h2>Edit Task</h2>
<br>
<form method="POST">
{% for field in form %}
<div class="form-group">
{{ field.label }}:
{{ field(class="form-control") }}
{% for error in field.errors %}
<span style="color: red;">*{{ error }}</span>
{% endfor %}
</div>
{% endfor %}
<p><input class="btn btn-primary" type="submit" value="Submit"></p>
</form>
</div> <!-- /container -->
<hr>
</main>
{% endblock %}
The full example of the Starlette web application is in example folder.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wtforms_piccolo-0.2.1.tar.gz
.
File metadata
- Download URL: wtforms_piccolo-0.2.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cd7e6e88860a4dbfff9d7f70170867f435238452a3723ef5d192394749c6152 |
|
MD5 | dca1fa0c2a7a580675c4d16dfa6b5b09 |
|
BLAKE2b-256 | e34e724f736969d7714bfd434be72a9f9c6e93471c09bce6433bd21c307adef6 |
File details
Details for the file wtforms_piccolo-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: wtforms_piccolo-0.2.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b52ab4331117e5b2e8e4befb60d3c6da6d57d5cc213b3cd14dff0cb8a9d7879f |
|
MD5 | bf450825b135918554e7eb646808f76c |
|
BLAKE2b-256 | 5e8a87fa629ba34716de24ddeb64c4f14c6ed3a7a0be3193aba75b43da6adf6a |