I'm using FlaskWTF for my forms, and would like to set the default value of a SelectField dynamically with the value I get from the url. I tried all the different variations I found but none of them seem to work. I'm not getting any error, it's just not selecting the correct default value.
class DataDiskForm(FlaskForm):
disk_type = SelectField('Data Disk Type',
choices= [('standard_ssd', 'Standard SSD'),
('premium_ssd', 'Premium SSD'),
('standard_hdd', 'Standard HDD')])
This is my form.
First I tried to set the value with the process() function. From what I've found this would break the validate_on_submit() so I would like to avoid this. But it's not working.
selected_disk_type = request.args.get('disk_type', default='standard_ssd')
form = DataDiskForm()
form.disk_type.default = selected_disk_type
form.process()
Then, I tried setting the value at initialization.
selected_disk_type = request.args.get('disk_type', default='standard_ssd')
form = DataDiskForm(disk_type=selected_disk_type)
and setting the data like this:
selected_disk_type = request.args.get('disk_type', default='standard_ssd')
form = DataDiskForm()
form.disk_type.data = selected_disk_type
None of them seem to work. I'm getting a correct value from the url query, for example: "premium_ssd", and I tried it by writing a literal string there but the default selected option is still the first option from the list.
How can I solve this?
Related
RefferCheck = Users.objects.get(username=str(request.POST['reffer']))
if RefferCheck:
if empty(RefferCheck.left):
RefferCheck.left = str(request.POST['username'])
RefferCheck.save()
I am trying to update a single row in Database. but it not working and don't show any error Too.
Note : I am not using Django forms i am using custom html forms.
First of all, you do not need this exctra check
RefferCheck = Users.objects.get(username=str(request.POST['reffer']))
if RefferCheck:
because get() method will raise DoesNotExists exception if no regord will be found.
Next thing is that your empty function is not needed and not working correctly. You can simply replace it with this:
# if left is None or empty string - this will evaluate to True
if not RefferCheck.left:
...
I am using the python salesforce toolkit for SFDC integration with a home grown application. We have nightly routines to read and write to SFDC. Based on a condition, I need to set the date value to current date or reset it to blank. I can set the date value, no issues. But I am unable to set the value to blank.
data = {}
if condition==True:
data['Termination_Date__c'] = '2017-05-12'
else:
data['Termination_Date__c'] = ??
I've tried to use '', 'null' and None while trying to blank out the value but nothing works so far. I am sure it's an easy solution but just can't find a way out. Any help would be appreciated.
Salesforce reads NULL values as None. try:
data['Termination_Date__c'] = None
Although, when I tried:
sf.Contact.update(data[0],{'Email' : Email1, 'MailingStreet' : ''})
It set my MailingStreet to nothing at all so '' seemed to have worked for me.
I found a solution to the question I posted. It's true that you can set '' to string fields to update them in SFDC. But certain fields like Date won't accept an empty string because of the validation rules in SFDC. A python None doesn't work either. But the toolkit has a way to handle such situation. Here's how you can blank/null the field in SFDC:
data = {}
if condition==True:
data['Termination_Date__c'] = '2017-05-12'
else:
data['fieldsToNull'] = ['Termination_Date__c']
I'm new to Python and I'm trying to make a simple bulletin board system app using web2py. I am trying to add a post into a certain board and I linked the post and board by including the following field in my post table: Field('board_id', db.board). When I try to create a post inside a particular board it gives me an error: "OperationalError: no such column: board.id". My code for create_posts:
def add_post():
board = db.board(request.args(0))
form = SQLFORM(db.post)
db.pst.board_id.default = db.board.id
if form.process().accepted:
session.flash = T('The data was inserted')
redirect(URL('default', 'index'))
return dict(form=form, board=board)
When I try to do {{=board}} on the page that shows the posts in a certain board, I get Row {'name': 'hi', 'id': 1L, 'pst': Set (pst.board_id = 1), 'description': 'hi'} so I know it's there in the database. But when I do the same thing for the "add post" form page, it says "board: None". I'm extremely confused, please point me in the right direction!
There appear to be several problems with your function. First, you are assigning the default value of the board_id field to be a Field object (i.e., db.board.id) rather than an actual id value (e.g., board.id). Second, any default values should be assigned before creating the SQLFORM.
Finally, you pass db.post to SQLFORM, but in the next line, the post table appears to be called db.pst -- presumably these are not two separate tables and one is just a typo.
Regarding the issue of {{=board}} displaying None, that indicates that board = db.board(request.args(0)) is not retrieving a record, which would be due to request.args(0) itself being None or being a value that does not match any record id in db.board. You should check how you are generating the links that lead to add_post and confirm that there is a valid db.board id in the first URL arg. In any case, it might be a good idea to detect when there is no valid board record and either redirect or display an error message.
So, your function should look something like this:
def add_post():
board = db.board(request.args(0)) or redirect(URL('default', 'index'))
db.pst.board_id.default = board.id
form = SQLFORM(db.pst)
if form.process(next=URL('default', 'index'),
message_onsuccess=T('The data was inserted'))
return dict(form=form, board=board)
Note, if your are confident that links to add_post will include valid board IDs, then you can eliminate the first line altogether, as there is no reason to retrieve a record based on its ID if the only field you need from it is the ID (which you already have). Instead, the second line could be:
db.pst.board_id.default = request.args(0) or redirect(URL('default', 'index'))
For below example:
Product(form):
product = TextField('name')
If i set this field in GET action
form.product.data= "123",
render is "123".
However, if i try set this value after POST action,
i allways get value form POST
How can i set this value (rerender) after POST ?
I wanted change only particulars fields, but rest keep from POST.
I have noticed that data from POST has additional field "raw_data" and set form.product.data fiels hasn't done (re)render. Solution proved to be clear
form.product.raw_data = None
form.product.data = 123
render new value
Maybe little bit "elegant" , but works !!!
Are you asking how to clear form data from the form after a user has submitted it?
In that case you could re-initialize the form
when you do
product = Product(request.POST)
It will fill the data that the user submitted.
product = Product()
This will clear the data.
Note:
use 4 spaces for every line of the code, so it is displayed well.
I have the following form,
class AddForm(wtf.Form):
tags = TagListField("Tags (comma separated)", validators=[wtf.Required()])
question = wtf.TextField("Question", validators=[wtf.Required()])
answers = wtf.FieldList(wtf.TextField("Answer", validators=[wtf.Required()]), min_entries=2, max_entries=5)
And I have a form setup to display this form along with a button that adds more "answers" inputs dynamically (by the user clicking a button). However, when the form gets submitted, any fields that are added but not filled in are considered errors.
Specifically, if I have 3 inputs for "Answer", but I only fill in the first two, then the third one comes up as an error, even though I have specified that the minimum number of entries is 2. It seems like it should ignore this data.
Since I am using this with Flask I am going to just modify the request.form data to ignore blank fields. Is there something I'm missing?
May be your the
validators=[wtf.Required()])
is an issue ? Have you tried changing it to
validators=[wtf.Optional()])