Note that there are some explanatory texts on larger screens.

plurals
  1. POflask-wtf form validation not working?
    primarykey
    data
    text
    <p>I've used flask before and I've had working form validation but for some reason its not working for my new app. Here is the basic code of the form </p> <pre><code>from flask.ext.wtf import Form, TextField, TextAreaField, SubmitField, validators,ValidationError class subReddit(Form): subreddit = TextField('subreddit', [validators.Required('enter valid subreddit')]) next = SubmitField('next') change = SubmitField('change') user = TextField('user', [validators.Required('enter valid user')]) fetch = SubmitField('fetch comments') </code></pre> <p>I have subreddit as the validation field, so if its empty, I want it to throw an error and reload the page. </p> <p>Here is the html</p> <pre><code>&lt;form class='sub' action="{{ url_for('sr') }}" method='post'&gt; {{ form.hidden_tag() }} &lt;p&gt; if you want to enter more than one subreddit, use the + symbol, like this: funny+pics+cringepics &lt;p&gt; &lt;br/&gt; {% for error in form.subreddit.errors %} &lt;p&gt;{{error}}&lt;/p&gt; {% endfor %} {{form.subreddit.label}} {{form.subreddit}} {{form.change}} &lt;/form&gt; </code></pre> <p>I have CSRF_ENABLED=True in my routes.py as well. What am I missing? When I leave the subredditfield empty and click change, it just reloads the page, no errors. This is an issue because whatever is in the field will get recorded in my database, and it can't be empty. </p> <p>EDIT</p> <pre><code>@app.route('/index',methods=['GET','POST']) @app.route('/',methods=['GET','POST']) def index(): form = subReddit() rand = random.randint(0,99) sr = g.db.execute('select sr from subreddit') srr = sr.fetchone()[0] r = requests.get('http://www.reddit.com/r/{subreddit}.json?limit=100'.format(subreddit=srr)) j = json.loads(r.content) pic = j['data']['children'][rand]['data']['url'] title = None if form.validate_on_submit(): g.db.execute("UPDATE subreddit SET sr=(?)", [form.subreddit.data]) print 'validate ' if j['data']['children'][rand]['data']['url']: print 'pic real' sr = g.db.execute('select sr from subreddit') srr = sr.fetchone()[0] r = requests.get('http://www.reddit.com/r/{subreddit}.json?limit=100'.format(subreddit=srr)) pic = j['data']['children'][rand]['data']['url'] title = str(j['data']['children'][rand]['data']['title']).decode('utf-8') return render_template('index.html',form=form,srr=srr,pic=pic,title=title) else: print 'not valid pic' return render_template('index.html',form=form,srr=srr,pic=pic) else: print 'not valid submit' return render_template('index.html',form=form,srr=srr,pic=pic) return render_template('index.html',form=form,srr=srr,pic=pic) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload