File tree Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 11import datetime
22
33from flask_mongoengine import MongoEngine
4+ from flask_mongoengine .wtf .orm import model_form
45
56db = MongoEngine ()
67
@@ -10,3 +11,6 @@ class Todo(db.Document):
1011 text = db .StringField ()
1112 done = db .BooleanField (default = False )
1213 pub_date = db .DateTimeField (default = datetime .datetime .now )
14+
15+
16+ TodoForm = model_form (Todo )
Original file line number Diff line number Diff line change 1717 </ div >
1818{% endmacro %}
1919
20+ {% macro with_errors(field) %}
21+ < div class ="form_field ">
22+ {% if field.errors %}
23+ {% set css_class = 'has_error ' + kwargs.pop('class', '') %}
24+ {{ field.label }}{{ field(class=css_class, **kwargs) }}{% if field.flags.required %}*{% endif %}
25+ < ul class ="errors "> {% for error in field.errors %}
26+ < li > {{ error|e }}</ li > {% endfor %}</ ul >
27+ {% else %}
28+ {{ field.label }}{{ field(**kwargs) }}{% if field.flags.required %}*{% endif %}
29+ {% endif %}
30+ </ div >
31+ {% endmacro %}
32+
2033{% block body %}
2134
2235 < div >
4457 < div >
4558 {{ render_navigation(todos_page, "pagination") }}
4659 </ div >
47-
60+ < div >
61+ < form method ="POST " action ="{{ url_for( "pagination ") }}">
62+ {% for field in form %}
63+ {{ with_errors(field, style='font-weight: bold') }}
64+ {% endfor %}
65+ < input type ="submit " value ="Create ">
66+ </ form >
67+ </ div >
4868
4969{% endblock %}
Original file line number Diff line number Diff line change @@ -66,8 +66,13 @@ def index():
6666
6767def pagination ():
6868 """Return pagination demonstration page and data generation entrypoint."""
69+ form = models .TodoForm ()
70+
6971 with switch_db (models .Todo , "secondary" ):
72+ if request .method == "POST" :
73+ form .validate_on_submit ()
74+ form .save ()
7075 page_num = int (request .args .get ("page" ) or 1 )
7176 todos_page = models .Todo .objects .paginate (page = page_num , per_page = 3 )
7277
73- return render_template ("pagination.html" , todos_page = todos_page )
78+ return render_template ("pagination.html" , todos_page = todos_page , form = form )
You can’t perform that action at this time.
0 commit comments