Reputation: 508
I am trying to create a page that lists all information about an entity, then allows the user to add notes about the entity. I am using Flask on Google App Engine (using the boilerplate written by Kamal Gill).
My question is two-part: a) how do I query information about an entity and then post information using a form? b) How do I pass in the entity information to the form, so it edits the entity?
Here's what I've got:
#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()])
within 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))
The url rules:
# Add notes
app.add_url_rule('/providers/<agency>', 'add_notes', view_func=views.add_notes, methods=['GET', 'POST'])
The HTML I have right now is like this:
<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> </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>
This results in a trace-back error:
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)
This error happens only when add_notes() is given an argument.
I'm a beginner at python, so I've probably missed something very obvious. I know that the error stems from trying to create a form within a GET context, but I'm not really sure how to fix that. And I have no idea how to start working on my two big questions.
Thanks, and let me know if I should further clarify something!
Edit: I have been looking at this question on routing, but don't really understand it enough to tell if it would work for what I need to do.
Upvotes: 2
Views: 7751
Reputation: 2116
You have to pass the agency parameter to the add_notes view while building the url:
<form method="post" action="{{ url_for('add_notes',agency='something') }}">
Upvotes: 8