1

Here I have created flaskwtform which will accpet two dates which is working fine. I want datetime picker should give me complete date with d/m-y format with UTC timestamp.

flaskWTForm.py

from flask import Flask, render_template
from flask_wtf import FlaskForm
from datetime import date
from wtforms.fields.html5 import DateField
from wtforms.fields.html5 import DateTimeField

app = Flask(__name__)
app.config['SECRET_KEY']='secretkey'

class TestForm(FlaskForm):
    startdate = DateField('From Date')
    todate = DateField('To Date')


@app.route('/dateExample',methods=['GET','POST'])
def index():
    forms = TestForm()
    if forms.validate_on_submit():
        return 'From Date is : {} To Date is : {}'.format(forms.startdate.data, forms.todate.data)
    return render_template('dateExample.html',form=forms)

if __name__ =="__main__":
    app.run(debug=True,port=5000)

[dateExample.html]

<html>
<body>
<h1> Flask WFForm </h1>
<form method="POST" action="{{url_for('.index')}}">
 {{ form.csrf_token }}
{{ form.startdate.label }}
   {{ form.startdate }}
{{ form.todate.label }}
   {{ form.todate }}
<input type="submit" value="Submit">
</form>
</body>
</html>

1 Answer 1

0

UPDATE

This worked for me:

forms.py

class TestForm(FlaskForm):
    startdate = DateField('From Date')
    todate = DateField('To Date')
    submit = SubmitField('Submit')

test.html

<html>
<body>
{% if result %}
<h1>{{ result }}</h1>
{% else %}
<h1> Flask WFForm </h1>
<form method="POST" action="">
 {{ form.csrf_token }}
{{ form.startdate.label }}
   {{ form.startdate }}
{{ form.todate.label }}
   {{ form.todate }}
{{ form.submit() }}
</form>
{% endif %}
</body>
</html>

routes.py

@app.route('/test', methods=['GET','POST'])
def test():
    form = TestForm()

    if form.validate_on_submit():
        result = 'From Date is : {} To Date is : {}'.format(form.startdate.data.strftime('%d-%m-%y'), form.todate.data.strftime('%d-%m-%y'))
        return render_template('test.html', form=form, result=result)

    return render_template('test.html', form=form)
1
  • You are right format='%Y-%m-%d' . But when I am giving format='%d/%m/%y' It is not working and Not showing any output. Dec 9, 2018 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.