from flask import Flask, render_template | |
from flask_wtf import Form | |
from wtforms import StringField, FormField, FieldList, HiddenField | |
import calendar | |
app = Flask(__name__) | |
app.secret_key = 'SCRATCH' | |
def dow_name(dow): | |
return calendar.day_name[dow] | |
app.jinja_env.filters['dow'] = dow_name | |
class TimeForm(Form): | |
opening = StringField('Opening Hour') | |
closing = StringField('Closing Hour') | |
day = HiddenField('Day') | |
class BusinessForm(Form): | |
name = StringField('Business Name') | |
hours = FieldList(FormField(TimeForm), min_entries=7, max_entries=7) | |
@app.route('/', methods=['post','get']) | |
def home(): | |
form = BusinessForm() | |
if form.validate_on_submit(): | |
results = [] | |
for idx, data in enumerate(form.hours.data): | |
results.append('{day}: [{open}]:[{close}]'.format( | |
day=calendar.day_name[idx], | |
open=data["opening"], | |
close=data["closing"], | |
) | |
) | |
return render_template('results.html', results=results) | |
print(form.errors) | |
return render_template('home.html', form=form) | |
if __name__ == '__main__': | |
app.run(debug=True) |
<ul> | |
{% for line in results %} | |
<li>{{ line }}</li> | |
{% endfor %} | |
</ul> |
This comment has been minimized.
This comment has been minimized.
I'm not sure what I'm looking at here. Could you write a few sentences about it? |
This comment has been minimized.
This comment has been minimized.
If you ever revisit this, it'd be extremely helpful to see how you instantiate and persist the nested TimeForms. |
This comment has been minimized.
This comment has been minimized.
Is your question 'How do I store this in a database?' and then '... and display it for editing' later? If you want to explain your goal (or even your particular problem and goal) I can probably elaborate a bit. |
This comment has been minimized.
This comment has been minimized.
Thanks for the prompt response! I was just wondering if there was a simple, uniform way to use the nested form object to populate a new instance of a model. I was figuring that WTForms would handle this mapping (via something like its |
This comment has been minimized.
This comment has been minimized.
I can't say I've ever tried- but I might give it a kick a bit later, seems like it should be possible No promises! |
This comment has been minimized.
This comment has been minimized.
Hey! That'd be great and I think it'd be very valuable to the community. I wound up finding a workable solution where I manually pluck values off of the nested form object and create a new model instance, but I suspect there is a more elegant solution:
|
This comment has been minimized.
This comment has been minimized.
Check out https://gist.github.com/doobeh/aa0e07892922cfa32123a6926c7720a5 and throw any questions you have there (I might have missed something obvious!) |
This comment has been minimized.
This comment has been minimized.
Hi it will be very helpfull to me too!!! app.jinja_env.filters['dow'] = dow_name |
This comment has been minimized.
This comment has been minimized.
Hi I have now understood the function and filter from using your code, But I need help if possible, |
This comment has been minimized.
This comment has been minimized.
The filter was more to give some data formatting on the template... it essentially just did a dict lookup {0: 'Monday', 1: 'Tuesday', ...} so the number provided by the index0 loop (0,1,2,3,4,5 ...) resolved to a word. |
This comment has been minimized.
This comment has been minimized.
Hi, Here is a link to my code.
https://gist.github.com/pascale64/b774de18c3deba47edca62fe7a6a882b
warm regards
Paul
…On Fri, 2020-03-06 at 18:53 -0800, Anthony Plunkett wrote:
The filter was more to give some data formatting on the template...
it essentially just did a dict lookup {0: 'Monday', 1: 'Tuesday',
...} so the number provided by the index0 loop (0,1,2,3,4,5 ...)
resolved to a word.
Feel free to post up an example of what you're having problems with--
and drop the link in here. Hopefully the
https://gist.github.com/doobeh/aa0e07892922cfa32123a6926c7720a5
example can provide more examples (without the filter confusing things)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
This comment has been minimized.
Thank you very much, helped me a lot.