Flask - Passing only selected fields












2















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()









share|improve this question

























  • 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, 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


















2















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()









share|improve this question

























  • 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, 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
















2












2








2








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()









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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





















  • 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, 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



















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














0






active

oldest

votes












Your Answer






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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)