Hi how can I get the variables in a HTTP post with WTForms when the post is done with a blobstoreuploadhandler and preferably also with i18n localized messages for validation?
This is my code that is not working:
class AdForm(Form):
name = TextField(_('Name'))
title = TextField(_('title'))
text = TextAreaField(_('Text'),widget=TextArea())
phonenumber = TextField(_('Phone number'))
phonenumberhide = BooleanField(_('Display phone number on site'))
price = TextField(_('Price'))
password = PasswordField(_('Password'))
email = TextField(_('Email'))
When I try to access the data posted via the form the data turns out as None:
form = AdForm(data=self.request.POST)
if form.title:
logging.info('getting title:'+form.title.data)
ad.title = form.title.data
ad.save()
The above does not save anything to the datastore and this is the template where it's coming from
<div class="labelform">
<div class="labelform" style="clear:left;">
<label> {% filter capitalize %}{% trans %}title{% endtrans %}{% endfilter %}:</label>
</div>
</div>
</td><td>
{{ form.title }}{% if form.title.errors %}
<ul class="errors">{% for error in form.title.errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}
Can you help me? There's something in the WTForms manual about appengine but I couldn't find a working example.
Update
I added validation tests and I still can't access the variables:
logging.info('getting requests')
if form.validate():
if form.title:
logging.info('getting title:'+form.title.data)
ad.title = form.title.data
ad.save()
ad.put()
Logging output:
INFO 2011-11-05 23:17:24,653 main.py:1504] getting requests INFO
2011-11-05 23:17:24,653 main.py:1507] getting title:
Update 2
I removed the WTForms dependence and it is still not working. The line logging.info('getting data:'+ self.request.get('title', '0')) only outputs 0 even though the form is just a regular http post form:
<form action="{{form_url}}" name="upload" method="post" enctype="multipart/form-data" accept-charset="utf-8">
Update 3
This minimal config with no WTForms and no Jinja works so it's probably something with Jinja when this bare-bones example works with webapp2 and python 2.7 where I'm going to add the faulty code line by line to troubleshoot:
class GuestPage(BaseHandler):
def get(self):
self.response.out.write("""
<html>
<body>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")
class Guestbook(BaseHandler, I18NHandler, blobstore_handlers.BlobstoreUploadHandler):
csrf_protect = False
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
self.response.out.write(self.request.get('content'))
self.response.out.write('</pre></body></html>')
app = webapp2.WSGIApplication([ ('/guest', GuestPage),
('/sign', Guestbook),
...
Update 4
My back to basics is working with Jinja so I suppose I just build on this example and see where it breaks:
class GuestPage(BaseHandler):
def get(self):
self.render_jinja('form_jinja')
class Guestbook(BaseHandler, I18NHandler, blobstore_handlers.BlobstoreUploadHandler):
csrf_protect = False
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
self.response.out.write(self.request.get('content'))
self.response.out.write('</pre></body></html>')
Update 5
I can reproduce the error with this minimal example that can't access the http post variable:
class GuestPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<body>
<form action=" """ +blobstore.create_upload_url('/sign')+ """ " method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")
class Guestbook(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
self.response.out.write(self.request.get('content'))
self.response.out.write('</pre></body></html>')
app = webapp2.WSGIApplication([ ('/guest', GuestPage),
('/sign', Guestbook),
Update 6
From the guestbook example code with blobstoreuploadhandler I can upload a file on the production server so I could make a working example that uses the blobstoreuploadhandler that I will try to build on for my use case.
Update 7
I could get my original code so that everything works except the blob transfer. I suspect a diff between dev_appserver and production that I posted to the google appengine group about. We'll see how it progresses.
Update 8
Here's another common use how nothing works when you add WTForms:
logging.info('getting data:'+ self.request.get('title', '0'))
logging.info('http post data:'+ str(self.request.post))
form = AdForm(formdata=self.request.data)
logging.info('populated form')
logging.info('form data:' + str(form.formdata))
if form.validate():
if form.title:
logging.info('getting title:'+str( form.get('title') ) )
ad.title = form.title.data ad.save() ad.put()
if form.text:
logging.info('getting text:' +str(form.text))
ad.text = form.text.data
if self.request.get('currency'):
ad.currency = self.request.get('currency')
if self.request.get('cg'):
ad.category = form.cg.data
if self.request.get('company_ad') == '1':
ad.company_ad = True
ad.put()
else:
logging.info('form did not validate')
except Exception, ex:
logging.info('there occured exception %s', str(ex))
INFO 2011-11-09 12:11:50,868 main.py:1385] getting data:TEST INFO
2011-11-09 12:11:50,868 main.py:1409] there occured exception post
Update 9
Finally the form populates it just doesn't validate. Thank you Sean for the info that got me further. Now I get past populated the form object with no exception but the exception occurs when I try to validate:
logging.info('getting data:'+ self.request.get('title', '0'))
form = AForm(self.request.POST)
logging.info('populated form')
if form.validate():
logging.info('validated form')
The above code is logging the output:
INFO 2011-11-11 08:03:59,913 main.py:1387] getting data:TEST
INFO 2011-11-11 08:03:59,914 main.py:1390] populated form
INFO 2011-11-11 08:03:59,914 main.py:1412] there occured exception 'builtin_function_or_method' object is not iterable
What does the exception mean?
My form class is
class AForm(Form):
name = TextField(_('Name'))
title = TextField(_('title'))
text = TextAreaField(_('Text'),widget=TextArea())
phonenumber = TextField(_('Phone number'))
phonenumberhide = BooleanField(_('Display phone number on site'))
price = TextField(_('Price'))
password = PasswordField(_('Password'))
email = TextField(_('Email'))
category = SelectField(choices=categories.keys)
I don't know anything about WTForm, but I'd guess that like Django forms, you need to call the validation function before you can access the data. In this case, it's form.validate():
form = AdForm(formdata=self.request.POST)
if form.validate():
ad.title = form.title.data
Daniel actually it is not data=self.request.POST that you need to pass to the form but formdata instead of data
http://wtforms.simplecodes.com/docs/dev/forms.html#the-form-class
hope it will be usefull for all those who rushed through the doc as i did
Related
When the user selects the translator name, I want to access the selected translator in Django by the id and store the information in an appointment table in the database, but there is an error when he tries to mach the id the error is: Field 'id' expected a number but got ''.
view.py: where I took the user's chosen language and did a filler for the manager.
def customerHomePage(request):
if request.method == 'POST':
search_id = request.POST.get('textfield', None)
try:
pro = Manager.objects.filter(Second_Language = search_id)
x={'pro': pro}
return render(request,'representTranslator.html', x)#for show the customer Home page
except pro.DoesNotExist:
return HttpResponse("no such user")
else:
return render(request, 'customerHome.html')
HtML code: I displayed only the manager with the same chosen language to prompt the user to send a new message.
<section style="margin-top: 10%;">
<form method="POST" action="/waite" >
{% csrf_token %}
<select id="translator" name="translator">
{% for i in pro %}
<option value="{{ i.id }}"> {{ i.name }}</option>
{% endfor %}
</select>
<button type="submit">Send request</button>
</form>
</section>
in django view.py:
def waitePage(request):
if request.method == 'POST':
id = request.POST.get("translator")
translator = Manager.objects.get(user_id=id)
translatorName= translator.name #get the name of the translator
translatorID= translator.id #get the id of the translator
current_user = request.user #to get the user
current_userId = current_user.id #for storing user id
customer = Customer.objects.get(id=current_userId) #to get the info of translator
customerID=customer.id
customerName=customer.name
appointment = Appointment.objects.create(
customerName=customerName,
customerID=customerID, translatorID=translatorID, accepted=False,)
appointment.save()
return render(request,'waitePage.html')
The error comes from:
translator = Manager.objects.get(user_id=id)
Do you guys have any idea how I can solve it?
Do you guys have any idea how I can solve it?
Instead of this:
<form method="POST" action="/waite" >
try this one:
<form method="POST" action="{% url 'waitePage' %}">
Django use Jinja templating, that may be the reason why you don't get id.
I'm trying to set up a basic "Contact" form for my website, which will basically take in values and then put them into a CSV file. The issue I'm having is that the entries cannot be validated cause they're missing a csrf_token?
Here's the relevant code from my app.py:
#app.route('/contact_end', methods=['POST'])
def handle_contact():
form = ContactForm()
print(form.name.data)
if form.validate_on_submit():
print("yup")
with open('data/messages.csv', 'a') as f:
print("oh shit")
writer = csv.writer(f)
writer.writerow([form.name.data, form.email.data, form.message.data])
print("waddup")
return redirect(url_for('contact_handler.html'), name=form.name.data)
print(form.errors)
return render_template('contact.html', form=form)
It skips over the if statement as it never ends out printing the "yup", and instead it prints out the error:
{'csrf_token': ['The CSRF token is missing.']}
The template that this connects to is:
{% extends "base_template.html" %}
{% block title %}Contact us {% endblock %}
{% block content %}
<p>Feel free to use the contact form below to send us any questions you might have.</p></br>
<form action="/contact_end" method="post">
{{ form.csrf_token }}
<label>Your Name <input type="text" name="name"/></label></br>
<label>Your Email <input type="text" name="email"/></label></br>
<label>Your Name <textarea name="message"></textarea></label></br>
<button type="submit">Send</button>
<button type="reset">Clear</button>
</form>
{% endblock %}
I've tried messing with form.csrf_token and .hidden_tags(), but with no success.
As well, this is the initial part of app.py that brings you to the page in the first place, the one above is the endpoint for the form:
#app.route('/contact')
def contact():
return render_template('contact.html', form=form)
Finally, here's my ContactForm class:
class ContactForm(FlaskForm):
print("yep")
name = StringField('Name', validators=[InputRequired()])
email = EmailField('Email', validators=[InputRequired(), Email()])
message = TextAreaField('Message', validators=[InputRequired()])
I've made sure to set my secret key, as well. Anyone have any idea why this isn't working? Many thanks.
You still need to create the form instance in your contact() function:
#app.route('/contact')
def contact():
form = ContactForm()
return render_template('contact.html', form=form)
I have a rather silly question.
I am trying an app and have two forms on it:
Enter a zip code (submit button)
or
Login if you already have a user id. (submit button)
Now in my code (python using web.py framework) i have two def POST(self) but which one will the first form and the second login form call?
I am super confused. Now my friend tells me html cannot have two different forms.
Here is the code and the form is incomplete - i am just trying to get the methods running well before I start grabbing more data and building a db schema.
import web
from web import form
render = web.template.render('templates/')
urls = (
'/', 'index'
)
myform = form.Form(
form.Textbox("Zip Code",
form.regexp('^\d{5}$', 'Not a zip code'),
description='Enter a 5 digit zip code',
maxlength = '5'),
)
myloginform = form.Form(
form.Textbox("Username",
form.regexp('^[^<>\s\#]+(\#[^<>\s\#]+(\.[^<>\s\#]+)+)$', 'Invalid username'),
description='Enter your username'),
)
class index:
def __init__(self): ** i still dont know wtf this does..
pass
def GET(self):
form = myform()
myloginform1 = myloginform()
return render.index(form,myloginform1)
def POST(self):
form = myform()
if not form.validates():
return render.index(form)
else:
return "The zip code you are located is: %s" % (form['Zip Code'].value)
def POST(self):
myloginform1 = myloginform()
if not myloginform1.validates():
return render.index(myloginform1)
else:
return "Welcome %s" % (myloginform1['Username'].value)
class testfunc:
def GET(self):
return "Test function returning!"
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
The index.html is below
$def with (form, myloginform1)
<html>
<head><title>8reps Welcome.. </title></head>
<body>
<h3>Hi</h3>
<p> Lets find some results in your area.. </p>
<form name="main" method="post">
$if not form.valid: <p class="error">Try again...</p>
$:form.render()
<input type="submit" /> </form>
<p> Already registered? Login as a user..</p>
<form name="login" method="post">
$if not myloginform1.valid: <p class="error">Try again..</p>
$:myloginform1.render()
<input type="submit" />Login</form>
</form>
</body>
</html>
Thank you!!
you may have as many forms on the page as you want, the only issue is how you will handle them.
there are 2 options:
two separate backends e.g. /login and /zip to handle POSTs from forms - in the form tag you will need to add action attribute to direct POSTs properly
single /index where you can handle both forms, but then you need to recognize which form was posted, e.g. by presence of some named field (you can use submit input with the same name but different values:
<input name="submit" type="submit" value="Zip" />
and for the second form:
<input name="submit" type="submit" value="Login" />
then you will know which form was posted by checking value of sent "submit" field.
for sure, you can't have 2 methods wit the same name in class definition.
I want to add unit tests to my flask app that tests form behavior on valid and invalid logins + signups. Currently, I have the signup form and a login form hosted on one page and route, and am using a hidden input field to identify which of the two forms is submitted / determine next actions.
My question is - how do I write a unit test that targets a specific form on a page? All the examples I've seen so far post data to a specific route, which is currently what I am doing. But that is failing because I need an added way to say "and we're submitting x form".
So is there a way to add "and we're submitting x form" in the post request?
**
edited to add, here are the different ways I've tried to pass the hidden form data in the post data dict, with no success.
data = dict(username="test#gmail.com", password="test", login_form)
data = dict(username="test#gmail.com", password="test", "login_form")
data = dict(username="test#gmail.com", password="test", login_form=True)
login unit test:
from app import app
import unittest
class FlaskTestCase(unittest.TestCase):
#ensure that login works with correct credentials
def test_correct_login(self):
tester = app.test_client(self)
response = tester.post(
'/login',
data = dict(username="test#gmail.com", password="test"),
follow_redirects=True
)
self.assertIn(b'you are logged in', response.data)
login route in views.py:
#app.route('/login', methods=['POST', 'GET'])
def login():
login_form = LoginForm()
signup_form = SignupForm()
error_login = ''
error_signup = ''
#login form
if 'login_form' in request.form and login_form.validate():
# do login form stuff
#signup form
if 'signup_form' in request.form and signup_form.validate():
# do signup form stuff
return render_template('login.html', login_form=login_form, signup_form=signup_form, error=error)
login.html:
<div class="login-form form-400">
<h3>Log In To Your Account</h3>
<form action="" method="post">
<input type="hidden" name="login_form">
{% if error_login != '' %}
<label class="error">
{{ error_login }}
</label>
{% endif %}
{% from "_formhelper.html" import render_field %}
{{ login_form.hidden_tag() }}
{{ render_field(login_form.email, placeholder="Your Email", class="form-item__full", type="email") }}
{{ render_field(login_form.password, placeholder="Your Password", class="form-item__full") }}
<input type="submit" value="Login" class="button button-blue">
</form>
</div>
<p class="login-divider">or</p>
<div class="signup-form form-400">
<h3>Create a New Account</h3>
<form action="" method="post">
<input type="hidden" name="signup_form">
{% if error_signup != '' %}
<label class="error">
{{ error_signup | safe}}
</label>
{% endif %}
{% from "_formhelper.html" import render_field %}
{{ signup_form.hidden_tag() }}
{{ render_field(signup_form.username, placeholder="Pick a Username", class="form-item__full") }}
{{ render_field(signup_form.email, placeholder="Your Email", class="form-item__full", type="email") }}
{{ render_field(signup_form.password, placeholder="Create a Password", class="form-item__full") }}
<input type="submit" value="Sign Up" class="button button-green">
</form>
</div>
Ok I figured it out. To pass the login_form info, I had to end up passing an empty string on the login_form like this:
def test_correct_login(self):
tester = app.test_client(self)
response = tester.post(
'/login',
data = dict(username="test#gmail.com", password="test", login_form=""),
follow_redirects=True
)
self.assertIn(b'you are logged in', response.data)
I did this by throwing a print request.form in my views.py for this route and then saw the empty string.
It was still failing, but because the login_form.validate() was failing because of the csrf token added by the WTForms module. In the end, this discussion had the answer.
Flask-WTF / WTForms with Unittest fails validation, but works without Unittest
Thanks #drdrez for your suggestions!
Update:
Thanks for updating your question with what you've already tried! I have a few other ideas about what's causing the issue.
Let's continue to look at the HTML and try to understand the technologies your program is built on top of. In the server side login.html file, notice these lines:
{% from "_formhelper.html" import render_field %}
{{ login_form.hidden_tag() }}
{{ render_field(login_form.email, placeholder="Your Email", class="form-item__full", type="email") }}
{{ render_field(login_form.password, placeholder="Your Password", class="form-item__full") }}
It isn't HTML, and is probably being processed on the server side to produce HTML and serve to the client. The line that contains login_form.hidden_tag() looks interesting, so I would recommend loading this page in your browser and inspecting the HTML served to the client. Unfortunately, I haven't used Flask before, so I can't give any more direct help.
However, my advice is to continue digging into how Flask and the HTML Form works. The nice thing about Python is you have access to libraries' source code, which allows you to figure out how they work so you can learn how to use them and fix bugs in your application that uses them.
Sorry I can't give you more direct help, good luck!
Let's look at login.html. When you submit a form, how does the login route in views.py know which form was submitted? If you know HTML Forms, <input> elements nested in a form are used to, in this case, post data to your server/application.
Back to login.html, notice these two lines:
...
<h3>Log In To Your Account</h3>
<input type="hidden" name="login_form">
...
<h3>Create a New Account</h3>
<form action="" method="post">
<input type="hidden" name="signup_form">
...
Those are <input> elements, with a type of "hidden", so they won't display, with names of "login_form" and "signup_form", which are included in the data that is submitted by the form.
Now in the login route in views.py, you'll notice there two lines:
#login form
if 'login_form' in request.form and login_form.validate():
# do login form stuff
#signup form
if 'signup_form' in request.form and signup_form.validate():
# do signup form stuff
Those are testing to see if the phrase "login_form" or "signup_form" are in present in the list request.form. Back to your unit test now:
response = tester.post(
'/login',
data = dict(username="test#gmail.com", password="test"),
follow_redirects=True
)
Notice the data you are passing in the dict, this is mimicking the form data, so you should probably include either "login_form" or "signup_form" to mimic the behavior of the HTML form correctly.
If you're unfamiliar with HTML Forms and HTTP Post, I would suggest searching for some tutorials, or just reading documentation on MDN or elsewhere. When building software on top of a technology (like HTTP and HTML), it can be helpful to understand how those technologies work when you run into bugs in your own software.
Hope this helps, let me know if I can clarify anything!
You might be experiencing a problem because you have not flagged the request as being of the form application content type. I note you are trying to access request.form, which requires that the data package is parsed in a certain way. You could try to do something like the following:
response = tester.post(
'/login',
data = dict(username="test#gmail.com", password="test"),
follow_redirects=True,
headers = {"Content-Type":"application/x-www-form-urlencoded"}
)
I have a python file and an html file that interact with each other through the jinja2 environment in a manner similar to the one in this tutorial.
The following code controls the interaction between the html file and the python file:
class MainPage(webapp2.RequestHandler):
def get(self):
submission_query = Submission.query().order(-Submission.score)
submissions = submission_query.fetch(10)
template_values = {
'Submission' : Submission,
'submissions' : submissions,
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/', MainPage),
('/create', CreateSubmmission),
('/voteup', VoteUp),
], debug=True)
I have a ndb model as follows:
class Submission(ndb.Model):
username = ndb.StringProperty()
placename = ndb.StringProperty()
link = ndb.StringProperty()
score = ndb.IntegerProperty()
I have an html form to create a new Submission as follows:
<form action="/create" method="post">
add a new location: <br>
your name:<div><textarea name="username" rows="2" cols="60"></textarea></div>
placename:<div><textarea name="placename" rows="2" cols="60"></textarea></div>
url:<div><textarea name="link" rows="2" cols="60"></textarea></div>
<div><input type="submit" value="Post"></div>
</form>
Using this request handler:
class CreateSubmmission(webapp2.RequestHandler):
def post(self):
submission = Submission()
submission.username = self.request.get('username')
submission.placename = self.request.get('placename')
submission.link = self.request.get('link')
submission.score = 0
submission.put()
self.redirect('/')
I have a section in my html which prints out each Submission along with a button for upvoting it:
{% for Submission in submissions %}
<p>
{{Submission.username}} posted:
<strong>{{Submission.placename}}</strong> <br>
score:{{Submission.score}}<br>
<!--Vote up button-->
<form action="/voteup?submission={{Submission}}" method="post">
<div><input type="submit" value="voteup"></div>
</form>
</p><br><br>
{% endfor %}
The upvoting is handled by the following python class:
class VoteUp(webapp2.RequestHandler):
def post(self):
submission = self.request.get('Submission')
submission_key = submission.put()
the_submission = submission_key.get()
the_submission.score +=1
the_submission.put()
self.redirect('/')
When pressing the button, the value of the respective Submission's score attribute should increase by one.
The code is implemented on the website sunlit-hook-91816.appspot.com. As can be seen on that site, pressing the upvote button generates the following error:
File "/base/data/home/apps/s~sunlit-hook-91816/1.383863233180213164/guestbook.py", line 52, in post
submission_key = submission.put()
AttributeError: 'str' object has no attribute 'put'
It appears that the VoteUp class is somehow unable to modify the value of Submission.score.
I am using the information found here but I can't figure out how to correctly apply it this problem. Can anyone tell me of a way to make the VoteUp class modify the Submission.score?
You're using the Submission object like it can be passed along between html/python code but it can't, you must add a reference in the form and dereference it back in you server.
Do this in the vote form:
<form action="/voteup?submission={{Submission.key.urlsafe()}}" method="post">
<div><input type="submit" value="voteup"></div>
</form>
We're getting the key of the submission in a format safe for http transport, so it can arrive back to the server unaltered.
Now when processing the vote, we can recreate the key and get the correct object:
class VoteUp(webapp2.RequestHandler):
def post(self):
submission = self.request.get('submission')
submission_key = ndb.Key(urlsafe=submission)
the_submission = submission_key.get()
the_submission.score +=1
the_submission.put()
self.redirect('/')
As you can see we're just recreating the key based on the formatted string we printed in the form.
Another way you could do this to make it even better is to use an extra input in the form:
<form action="/voteup" method="post">
<input name="submission" type="hidden" value="{{Submission.key.urlsafe()}}">
<div><input type="submit" value="voteup"></div>
</form>
Your code will work the same way, but I find it easier to read and maintain.