小编pru*_*iap的帖子

当我使用Flask的POST时,我遇到了wtforms selectfields的问题

我对wtforms和flask很新,并且正在搞乱selectfields并且出错了.表单本身在没有selectfield的情况下工作得很好但是有了它我得到以下错误:

错误:

....fields.py", line 386, in pre_validate
    for v, _ in self.choices: TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)

我看到了选择字段,所以它正在被渲染.我怀疑在POST上没有正确验证id并且没有返回任何内容.或者它与我的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)
Run Code Online (Sandbox Code Playgroud)

myforms.py:

class PostForm(Form):
    title = wtf.TextField('Title', validators=[validators.Required()])
    content = wtf.TextAreaField('Content', validators=[validators.Required()])
    hometest = wtf.SelectField(u'Home …
Run Code Online (Sandbox Code Playgroud)

python flask wtforms flask-wtforms

21
推荐指数
2
解决办法
2万
查看次数

了解 ndb 密钥类与 KeyProperty

我已经浏览了文档、文档以及 SO 问题和答案,但仍在努力理解其中的一小部分。您应该选择哪个,何时选择?

这是我到目前为止所读到的(只是示例):

关键类对我来说似乎很简单。当您创建 ndb 实体时,数据存储会自动为您创建一个密钥,通常采用 key(Kind, id) 的形式,其中 id 是为您创建的。

所以说你有这两个模型:

class Blah(ndb.Model):
     last_name = ndb.StringProperty()

class Blah2(ndb.Model):
     first_name = ndb.StringProperty()
     blahkey = ndb.KeyProperty()
Run Code Online (Sandbox Code Playgroud)

因此,只需使用密钥类型,您就想让 Blah1 成为父母(或有几个姓氏相同的家庭成员)

lname = Blah(last_name = "Bonaparte")
l_key = lname.put() **OR**
l_key = lname.key.id() # spits out some long id 

fname_key = l_key **OR**
fname_key = ndb.Key('Blah', lname.last_name) # which is more readable.. 
Run Code Online (Sandbox Code Playgroud)

然后:

lname = Blah2( parent=fname_key, first_name = "Napoleon")
lname.put()

lname2 = Blah2( parent=fname_key, first_name = "Lucien") …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine app-engine-ndb google-cloud-datastore

5
推荐指数
1
解决办法
4929
查看次数

谷歌/ apis/drive_v2新的谷歌驱动器身份验证身份验证.我也希望避免对google-api-client感到沮丧

首先我的问题是关于使用我的rails 4.2 app获取谷歌drive_v2身份验证和创建新文件等.我有使用devise/omniauth2的rails应用程序和谷歌工作正常.我在google开发者控制台中激活了web auth和google API身份验证.两个客户端机密都已下载并正常工作.

所以说了.我需要离线访问我的rails应用程序才能使用,创建和删除goole Docs和Spreadsheets.我已经设置了.drive的范围,设置了离线访问等.

如果我进入谷歌游乐场并获得一个新的访问令牌,我甚至编写了可以正常工作的代码.

我的第一个问题是能够使用google-api-client获取我的refresh_token并进行身份验证或获取新版本(版本0.9pre3,这样人们就不会感到困惑)

为了使用新的谷歌API我需要评论谷歌驱动器,因为它似乎所有的API组合到客户端.

gem 'omniauth-google-oauth2'
#gem 'google_drive'
gem 'google-api-client', '0.9.pre3'
Run Code Online (Sandbox Code Playgroud)

我坚持认证/授权步骤,我对此感到相当愚蠢.

在GitHub库的API,因此示例代码在这里,或谷歌开发站点这里使我相信这个工程.

require 'google/apis/drive_v2'

Drive = Google::Apis::DriveV2 # Alias the module
drive = Drive::DriveService.new
drive.authorization = authorization # See Googleauth or Signet libraries
Run Code Online (Sandbox Code Playgroud)

要么

drive.authorization = Google::Auth.get_application_default([Drive::AUTH_DRIVE])
Run Code Online (Sandbox Code Playgroud)

当然什么是授权?根据该文档,我去这里.

运行以下示例代码:require'googleauth'

获取环境配置的授权

scopes =  ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/compute']
authorization = Google::Auth.get_application_default(scopes)
Run Code Online (Sandbox Code Playgroud)

导致:

authorization = Google::Auth.get_application_default(scopes)
RuntimeError: Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials
for more information
from /Users/prussiap/.gem/ruby/2.2.0/gems/googleauth-0.4.2/lib/googleauth.rb:116:in `get_application_default' …
Run Code Online (Sandbox Code Playgroud)

ruby google-api-client google-drive-api ruby-on-rails-4

5
推荐指数
2
解决办法
1251
查看次数