2

我正在尝试创建一个列出有关实体的所有信息的页面,然后允许用户添加有关实体的注释。我在 Google App Engine 上使用 Flask(使用由 Kamal Gill 编写的样板文件)。

我的问题分为两部分:a)如何查询有关实体的信息,然后使用表单发布信息?b)如何将实体信息传递给表单,以便编辑实体?

这是我所拥有的:

#model.py
class Providers(db.Model):
    """Provider Information DB"""
    pAgency = db.StringProperty()
    pSite = db.StringProperty()
    programName = db.StringProperty()
    pNotes = db.TextProperty()

#forms.py
class ProviderForm(wtf.Form):
    providerName = wtf.TextField()
    providerNote = wtf.TextAreaField('Add notes', validators=[validators.Required()])

在views.py中

def list_addresses(agency):
  addrs = []
  addresses = db.Query(Providers).filter('pAgency =', agency).order('pAddress')
  for addr in addresses:
     addrs.append((addr.pAddress, addr.programName))
  return addrs

def add_notes(agency):
  form = ProviderForm()
  if form.validate_on_submit():
     notes = ProviderNotes(
             pNotes = form.providerNote.data)
     try:
       notes.put()
       return 'Worked'
     except:
       return redirect(url_for('list_provs'))
  return render_template('provider_notes.html', form=form, addrs=list_addresses(agency))

网址规则:

# Add notes
app.add_url_rule('/providers/<agency>', 'add_notes', view_func=views.add_notes, methods=['GET', 'POST'])

我现在拥有的 HTML 是这样的:

<ul>
 <h1 id="">Provider Information</h1>
     {% for address in addrs %}
     <p>{{address}}</p>
        {% for prog in programs %}
          {% if prog != '' %}
           <li>{{prog}}</li>
          {% endif %}
        {% endfor %}
     <p>&nbsp;</p>
     {% endfor %}
</ul>
<form method="post" action="{{ url_for('list_addresses') }}">
{% for field in form %}
        <p>{{ field.label }}</p>
        <p>{{ field }}</p>
{% endfor %}
    <p>
        <button type="submit">Save notes</button>
    <a href="{{ url_for('list_provs') }}">Cancel</a>
    </p>
</form>

这会导致回溯错误:

Traceback (most recent call last):
File ".../src/packages/flaskext/gae_mini_profiler/profiler.py", line 303, in __call__
  result = self.app(environ, start_response)
File ".../src/packages/flask.zip/flask/app.py", line 1506, in wsgi_app
File ".../src/packages/flask.zip/flask/app.py", line 1504, in wsgi_app
File ".../src/packages/flask.zip/flask/app.py", line 1264, in full_dispatch_request
File ".../src/packages/flask.zip/flask/app.py", line 1262, in full_dispatch_request
File ".../src/packages/flask.zip/flask/app.py", line 1248, in dispatch_request
File ".../src/application/views.py", line 55, in add_notes
  return render_template('provider_notes.html', form=form, addrs=list_addresses(agency))
File ".../src/packages/flask.zip/flask/templating.py", line 123, in render_template
File ".../src/packages/flask.zip/flask/templating.py", line 107, in _render
File ".../src/packages/jinja2.zip/jinja2/environment.py", line 891, in render
File ".../src/application/templates/provider_notes.html", line 1, in top-level template code
  <form method="post" action="{{ url_for('add_notes') }}">
  BuildError: ('add_notes', {}, None)

仅当为 add_notes() 提供参数时才会发生此错误。

我是 python 的初学者,所以我可能错过了一些非常明显的东西。我知道该错误源于尝试在 GET 上下文中创建表单,但我不确定如何解决该问题。而且我不知道如何开始解决我的两个大问题。

谢谢,如果我应该进一步澄清一些事情,请告诉我!

编辑:我一直在看这个关于 routing的问题,但并没有真正理解它是否适​​用于我需要做的事情。

4

1 回答 1

8

您必须在构建 url 时将代理参数传递给 add_notes 视图:

<form method="post" action="{{ url_for('add_notes',agency='something') }}">
于 2012-04-19T17:03:13.253 回答