Flask - Passing only selected fields
I have a flask webpage that asks for the user to select few fields and returns back output based on the fields selected.
The below code works just fine with 3 fields. If the user selects all the 3 fields there are no issues. However if any of the fields are not selected, it returns an error saying column "none" does not exist.
def template():
form = TestForm()
if form.validate_on_submit():
student_id = form.student_id.data
class_id = form.class_id.data
roll_id = form.roll_id.data
print(student_id)
print(class_id)
print(roll_id)
Could anyone assist on how I could modify this such that only fields selected are passed and the unselected fields are ignored. Thanks
Edit (HTML code):
<form action=""
enctype="multipart/form-data"
method="POST">
{{form.hidden_tag()}}
<table>
<div class="checkbox">
<label>
<input type="checkbox" name="student_id" value="student_id"> student_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="class_id" value="class_id"> class_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="roll_id" value="roll_id"> roll_id
</label>
</div>
<tr class="submit">
<td></td>
<td><button type="submit">Get the Report!</button>
</td>
</tr>
</table>
</form>
Query that runs after getting user input:
def function(*field_names):
cursor = conn.cursor()
cursor.execute('select {} from enrolments'.format(', '.join(str(field) for field in field_names)))
print(field_names)
output_file = dwh_cursor.fetchall()
python-3.x flask
add a comment |
I have a flask webpage that asks for the user to select few fields and returns back output based on the fields selected.
The below code works just fine with 3 fields. If the user selects all the 3 fields there are no issues. However if any of the fields are not selected, it returns an error saying column "none" does not exist.
def template():
form = TestForm()
if form.validate_on_submit():
student_id = form.student_id.data
class_id = form.class_id.data
roll_id = form.roll_id.data
print(student_id)
print(class_id)
print(roll_id)
Could anyone assist on how I could modify this such that only fields selected are passed and the unselected fields are ignored. Thanks
Edit (HTML code):
<form action=""
enctype="multipart/form-data"
method="POST">
{{form.hidden_tag()}}
<table>
<div class="checkbox">
<label>
<input type="checkbox" name="student_id" value="student_id"> student_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="class_id" value="class_id"> class_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="roll_id" value="roll_id"> roll_id
</label>
</div>
<tr class="submit">
<td></td>
<td><button type="submit">Get the Report!</button>
</td>
</tr>
</table>
</form>
Query that runs after getting user input:
def function(*field_names):
cursor = conn.cursor()
cursor.execute('select {} from enrolments'.format(', '.join(str(field) for field in field_names)))
print(field_names)
output_file = dwh_cursor.fetchall()
python-3.x flask
Show your HTML and your validation code. It sounds like you have your select field value defaulting to "none". You probably want to default it to an empty string or change your validation
– Cfreak
Nov 21 '18 at 18:30
@Cfreak have included the HTML code in my initial post.
– scott martin
Nov 21 '18 at 18:32
1
Show us how you callfunction
, also consider renaming it. Secondly, iffield_names
are coming directly from the value of your HTML, then this code is vulnerable to SQL Injection attacks. You want to specify the columns to select. Generally for checkboxes the value attribute should be a true or false value to decide if it's selected or not. The columns you select should be static and, you would use aWHERE
statement in your SQL to control which rows you get back.
– Cfreak
Nov 21 '18 at 18:54
@Cfreak, this function hits a temp table that would have maximum of 10 records and this will only fetch 3 samples from that list. Sure would rename the function appropriately. However I am stuck on how to get past these non selected fields that return 'None'
– scott martin
Nov 21 '18 at 18:57
add a comment |
I have a flask webpage that asks for the user to select few fields and returns back output based on the fields selected.
The below code works just fine with 3 fields. If the user selects all the 3 fields there are no issues. However if any of the fields are not selected, it returns an error saying column "none" does not exist.
def template():
form = TestForm()
if form.validate_on_submit():
student_id = form.student_id.data
class_id = form.class_id.data
roll_id = form.roll_id.data
print(student_id)
print(class_id)
print(roll_id)
Could anyone assist on how I could modify this such that only fields selected are passed and the unselected fields are ignored. Thanks
Edit (HTML code):
<form action=""
enctype="multipart/form-data"
method="POST">
{{form.hidden_tag()}}
<table>
<div class="checkbox">
<label>
<input type="checkbox" name="student_id" value="student_id"> student_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="class_id" value="class_id"> class_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="roll_id" value="roll_id"> roll_id
</label>
</div>
<tr class="submit">
<td></td>
<td><button type="submit">Get the Report!</button>
</td>
</tr>
</table>
</form>
Query that runs after getting user input:
def function(*field_names):
cursor = conn.cursor()
cursor.execute('select {} from enrolments'.format(', '.join(str(field) for field in field_names)))
print(field_names)
output_file = dwh_cursor.fetchall()
python-3.x flask
I have a flask webpage that asks for the user to select few fields and returns back output based on the fields selected.
The below code works just fine with 3 fields. If the user selects all the 3 fields there are no issues. However if any of the fields are not selected, it returns an error saying column "none" does not exist.
def template():
form = TestForm()
if form.validate_on_submit():
student_id = form.student_id.data
class_id = form.class_id.data
roll_id = form.roll_id.data
print(student_id)
print(class_id)
print(roll_id)
Could anyone assist on how I could modify this such that only fields selected are passed and the unselected fields are ignored. Thanks
Edit (HTML code):
<form action=""
enctype="multipart/form-data"
method="POST">
{{form.hidden_tag()}}
<table>
<div class="checkbox">
<label>
<input type="checkbox" name="student_id" value="student_id"> student_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="class_id" value="class_id"> class_id
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="roll_id" value="roll_id"> roll_id
</label>
</div>
<tr class="submit">
<td></td>
<td><button type="submit">Get the Report!</button>
</td>
</tr>
</table>
</form>
Query that runs after getting user input:
def function(*field_names):
cursor = conn.cursor()
cursor.execute('select {} from enrolments'.format(', '.join(str(field) for field in field_names)))
print(field_names)
output_file = dwh_cursor.fetchall()
python-3.x flask
python-3.x flask
edited Nov 21 '18 at 18:32
scott martin
asked Nov 21 '18 at 18:28
scott martinscott martin
1288
1288
Show your HTML and your validation code. It sounds like you have your select field value defaulting to "none". You probably want to default it to an empty string or change your validation
– Cfreak
Nov 21 '18 at 18:30
@Cfreak have included the HTML code in my initial post.
– scott martin
Nov 21 '18 at 18:32
1
Show us how you callfunction
, also consider renaming it. Secondly, iffield_names
are coming directly from the value of your HTML, then this code is vulnerable to SQL Injection attacks. You want to specify the columns to select. Generally for checkboxes the value attribute should be a true or false value to decide if it's selected or not. The columns you select should be static and, you would use aWHERE
statement in your SQL to control which rows you get back.
– Cfreak
Nov 21 '18 at 18:54
@Cfreak, this function hits a temp table that would have maximum of 10 records and this will only fetch 3 samples from that list. Sure would rename the function appropriately. However I am stuck on how to get past these non selected fields that return 'None'
– scott martin
Nov 21 '18 at 18:57
add a comment |
Show your HTML and your validation code. It sounds like you have your select field value defaulting to "none". You probably want to default it to an empty string or change your validation
– Cfreak
Nov 21 '18 at 18:30
@Cfreak have included the HTML code in my initial post.
– scott martin
Nov 21 '18 at 18:32
1
Show us how you callfunction
, also consider renaming it. Secondly, iffield_names
are coming directly from the value of your HTML, then this code is vulnerable to SQL Injection attacks. You want to specify the columns to select. Generally for checkboxes the value attribute should be a true or false value to decide if it's selected or not. The columns you select should be static and, you would use aWHERE
statement in your SQL to control which rows you get back.
– Cfreak
Nov 21 '18 at 18:54
@Cfreak, this function hits a temp table that would have maximum of 10 records and this will only fetch 3 samples from that list. Sure would rename the function appropriately. However I am stuck on how to get past these non selected fields that return 'None'
– scott martin
Nov 21 '18 at 18:57
Show your HTML and your validation code. It sounds like you have your select field value defaulting to "none". You probably want to default it to an empty string or change your validation
– Cfreak
Nov 21 '18 at 18:30
Show your HTML and your validation code. It sounds like you have your select field value defaulting to "none". You probably want to default it to an empty string or change your validation
– Cfreak
Nov 21 '18 at 18:30
@Cfreak have included the HTML code in my initial post.
– scott martin
Nov 21 '18 at 18:32
@Cfreak have included the HTML code in my initial post.
– scott martin
Nov 21 '18 at 18:32
1
1
Show us how you call
function
, also consider renaming it. Secondly, if field_names
are coming directly from the value of your HTML, then this code is vulnerable to SQL Injection attacks. You want to specify the columns to select. Generally for checkboxes the value attribute should be a true or false value to decide if it's selected or not. The columns you select should be static and, you would use a WHERE
statement in your SQL to control which rows you get back.– Cfreak
Nov 21 '18 at 18:54
Show us how you call
function
, also consider renaming it. Secondly, if field_names
are coming directly from the value of your HTML, then this code is vulnerable to SQL Injection attacks. You want to specify the columns to select. Generally for checkboxes the value attribute should be a true or false value to decide if it's selected or not. The columns you select should be static and, you would use a WHERE
statement in your SQL to control which rows you get back.– Cfreak
Nov 21 '18 at 18:54
@Cfreak, this function hits a temp table that would have maximum of 10 records and this will only fetch 3 samples from that list. Sure would rename the function appropriately. However I am stuck on how to get past these non selected fields that return 'None'
– scott martin
Nov 21 '18 at 18:57
@Cfreak, this function hits a temp table that would have maximum of 10 records and this will only fetch 3 samples from that list. Sure would rename the function appropriately. However I am stuck on how to get past these non selected fields that return 'None'
– scott martin
Nov 21 '18 at 18:57
add a comment |
0
active
oldest
votes
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418432%2fflask-passing-only-selected-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418432%2fflask-passing-only-selected-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Show your HTML and your validation code. It sounds like you have your select field value defaulting to "none". You probably want to default it to an empty string or change your validation
– Cfreak
Nov 21 '18 at 18:30
@Cfreak have included the HTML code in my initial post.
– scott martin
Nov 21 '18 at 18:32
1
Show us how you call
function
, also consider renaming it. Secondly, iffield_names
are coming directly from the value of your HTML, then this code is vulnerable to SQL Injection attacks. You want to specify the columns to select. Generally for checkboxes the value attribute should be a true or false value to decide if it's selected or not. The columns you select should be static and, you would use aWHERE
statement in your SQL to control which rows you get back.– Cfreak
Nov 21 '18 at 18:54
@Cfreak, this function hits a temp table that would have maximum of 10 records and this will only fetch 3 samples from that list. Sure would rename the function appropriately. However I am stuck on how to get past these non selected fields that return 'None'
– scott martin
Nov 21 '18 at 18:57