2013-04-10 40 views
13

我對wtforms和flask非常陌生,並且正在與selectfields搞混,並且出現錯誤。表單本身只是正常工作沒有selectfield但它我得到以下錯誤:我在使用wtforms selectfields時遇到問題當我使用帶有Flask的POST

錯誤:

....fields.py", line 386, in pre_validate 
    for v, _ in self.choices: TypeError: 'NoneType' object is not iterable 

我看到selectfield所以它的渲染。我懷疑這個ID在POST上沒有被正確驗證,並且沒有返回。或者它與我返回的selectfield元組有關?此外,我使用的ID字段是從GAE的ndb自動鍵()。id()中提取的,這是相當長和令人討厭的。它可能是用於選擇字段的ID長度太長?

谷歌搜索沒有提供太多的確切問題,所以想我會在這裏發佈。下面的相關代碼。如果我失去了一些東西,請讓我知道

views.py代碼:

@app.route('/new/post', methods = ['GET', 'POST']) 
@login_required 
def new_post(): 

    form = PostForm() 
    if form.validate_on_submit(): 
     post = Post(title = form.title.data, 
        content = form.content.data, 
        hometest = form.hometest.data, 
        author = users.get_current_user()) 
     post.put() 
     flash('Post saved on database.') 
     return redirect(url_for('list_posts')) 
    form.hometest.choices = [ (h.key.id(),h.homename)for h in Home.query()] 

    return render_template('new_post.html', form=form) 

myforms.py

class PostForm(Form): 
    title = wtf.TextField('Title', validators=[validators.Required()]) 
    content = wtf.TextAreaField('Content', validators=[validators.Required()]) 
    hometest = wtf.SelectField(u'Home Name List', coerce=int,validators=[validators.optional()]) 

new_post.html:

{% extends "base.html" %} 

{% block content %} 
    <h1 id="">Write a post</h1> 
    <form action="{{ url_for('new_post') }}" method="post" accept-charset="utf-8"> 
     {{ form.csrf_token }} 
     <p> 
      <label for="title">{{ form.title.label }}</label><br /> 
      {{ form.title|safe }}<br /> 
      {% if form.title.errors %} 
      <ul class="errors"> 
       {% for error in form.title.errors %} 
       <li>{{ error }}</li> 
       {% endfor %} 
      </ul> 
      {% endif %} 
     </p> 
     <p> 
      <label for="title">{{form.hometest.label}}</label><br/> 
      {{form.hometest}} 
      {% if form.hometest.errors %} 
     <ul class="errors"> 
      {% for error in form.hometest.errors %} 
      <li>{{ error }}</li> 
      {% endfor %} 
     </ul> 
     {% endif %} 
     </p> 
     <p> 
      <label for="title">{{ form.content.label }}</label><br /> 
      {{ form.content|safe }}<br /> 

      {% if form.content.errors %} 
      <ul class="errors"> 
       {% for error in form.content.errors %} 
       <li>{{ error }}</li> 
       {% endfor %} 
      </ul> 
      {% endif %} 
     </p> 
     <p><input type="submit" value="Save post"/></p> 
    </form> 
{% endblock %} 

回答

26

您需要前將選擇你叫validate_on_submit作爲form.validate將嘗試驗證所提供的值(如果有的話)對選擇列表(這是None你設置choices前):

form = PostForm() 
form.hometest.choices = [(h.key.id(), h.homename) for h in Home.query()] 

if form.validate_on_submit(): 
    # form is valid, continue 
+0

如何讓它跳過如果你不想驗證它的特定領域?我得到一個錯誤的日期字段與驗證= [validators.optional()] – moaglee 2016-05-13 12:34:56

+0

值得問一個單獨的問題,與一個最小的,可重複的例子。有人應該能夠幫助:-) – 2016-05-13 12:38:36

4

你應該提供choices=[...]的說法,像

wtf.SelectField(u'Home Name List', 
       choices=[(1, 'Label 1'), 
         (2, 'Label 2')], 
       coerce=int, 
       validators=[validators.optional()]) 
+2

看樣子你卻不能使用選項參數在selectField進行動態選擇,如文檔中所示,wtforms.readthedocs.io/en/latest/...,因爲即使你在select字段中的選擇參數是一個查詢,就像在Home.query()中的wtf.SelectField(u'Home Name List',choices = [(h.key.id(),h.homename) ,coerce = int)'查詢不會在每次顯示窗體時運行..所以動態添加的home實例不會出現在下拉列表中..從我測試過的 – 2016-11-29 18:43:57

相關問題