Last active
August 29, 2015 14:26
-
-
Save hello-josh/f7ca017f5bcb2497ad55 to your computer and use it in GitHub Desktop.
Using flask_wtf and wtforms_appengine to generate forms from an ndb.Model subclass with an ndb.KeyProperty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.appengine.ext import ndb | |
class AuditMixin(object): | |
"""Audit fields for datastore objects""" | |
created_at = ndb.DateTimeProperty(auto_now_add=True) | |
created_user = ndb.StringProperty(indexed=False) | |
modified_at = ndb.DateTimeProperty(auto_now=True) | |
modified_user = ndb.StringProperty(indexed=False) | |
class Themes(ndb.Model, AuditMixin): | |
name = ndb.StringProperty(required=True) | |
template = ndb.StringProperty(required=True) | |
class Article(ndb.Model, AuditMixin): | |
"""Entity that stores the article content and redirect URL""" | |
image_url = ndb.TextProperty(required=True) | |
summary = ndb.TextProperty(required=True) | |
redirect_url = ndb.StringProperty(required=True) | |
#: The date to display for the article. Defaults to now | |
story_date = ndb.DateTimeProperty(auto_now_add=True) | |
theme = ndb.KeyProperty(Themes, required=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "base.html" %} | |
{% block content %} | |
<form method="POST"> | |
{{ form.hidden_tag() }} | |
<table> | |
{% for field in form if field.name != 'csrf_token' %} | |
<tr> | |
<th>{{ field.label }}:</th> | |
<td>{{ field }}</td> | |
</tr> | |
{% endfor %} | |
</table> | |
</form> | |
{% endblock %} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask_wtf | |
from wtforms_appengine import ndb | |
import entities | |
ArticleForm = ndb.model_form(entities.Article, | |
base_class=flask_wtf.Form, | |
exclude=vars(entities.AuditMixin), | |
field_args={'theme': {'get_label': 'name'}}) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# snip from a flask app | |
@app.route('/form') | |
def form(): | |
"""Return a form.""" | |
return render_template('form.html', form=forms.ArticleForm()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST"> | |
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1438627230##43addfeb49214c338bf559c4daaf5d3a76525758"></div> | |
<table> | |
<tr> | |
<th><label for="image_url">Image Url</label>:</th> | |
<td><textarea id="image_url" name="image_url"></textarea></td> | |
</tr> | |
<tr> | |
<th><label for="summary">Summary</label>:</th> | |
<td><textarea id="summary" name="summary"></textarea></td> | |
</tr> | |
<tr> | |
<th><label for="redirect_url">Redirect Url</label>:</th> | |
<td><input id="redirect_url" name="redirect_url" type="text" value=""></td> | |
</tr> | |
<tr> | |
<th><label for="theme">Theme</label>:</th> | |
<td><select id="theme" name="theme"><option value="entertainment">Entertainment Gossip</option><option value="news">News Site</option></select></td> | |
</tr> | |
</table> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what it looks like in a browser
