Sample Form to Insert a Professional
Purpose:
Project Part 4 requires you to create admin pages where the user can add professionals, service categories, and services. Taking user data and acting on it requires several steps. Your job is to practice and understand each step. This exercise provides you a sample "add professional" page, patterned after your project. It uses a single form to add a professional. You are to test it, change it, understand it, and learn from it, so you can apply the principles to the more complicated "add services" page (which displays multiple forms, one allowing you to add a service category and also allows you to add a service into each category).
Download the Sample Code
This limited demo code functions the way your professionals_add page should. It deliberately isolates the part of the project that does this, stripping out most other functionality. The data_restore route works so you can reset the data easily. Unzip it and run it using the Flask dev server. Go to the professionals_add route. The Flask app responds with a page that lists professionals and displays a form to take user data for a new one. The form submits to the professionals_add route (POST request with data). The back end receives the data, validates it, takes back end actions (file and database), and then re-renders the page.
Deep Dive: Walk Through the Execution Stage by Stage
Take the following steps to help you understand each stage of execution before you may mark this exercise as done.
Stage 1: Form Page (GET Request).
- Open Dev Tools > Network Tab and then load the form page by going to the professionals_add route.
- Click on the first line of the Network tab (professionals_add).
- Look at the "Headers" subtab to see exactly what was requested. Look for the line that says the GET method was used and that no data was passed (when GET data is passed, it uses a ? in the URL).
- Look at the "Response" subtab to see exactly what the response was to the first line item.
- Look at all the other line-items on the Network tab that were requested and compare them to the HTML response of the first line item, so you understand exactly why the server requested all of it.
- Now look at the flask application code. Take a moment to look at which route responds to the GET request. Look at what variables are passed to render_template and how they are handled in the template file.
- Satisfy yourself that when you render this page and do nothing further, the request and response for the form is finished and neither the server nor the browser is doing anything at this point but listening for the next request.
Stage 2: Invalid Form Submission with NoValidate (POST Request).
- Keep dev tools open.
- Attempt to submit the form with invalid data (a blank name or no image file). Front-end validation should stop you.
- Now, turn off front-end validation by going to the Elements tab and adding the NoValidate attribute to the form.
- Go back to the Network tab (before or after submitting again; the result is the same so long as dev tools stay open).
- Submit the invalid data with NoValidate in Place. Front-end validation should not stop you. An error message should come from the back end.
- Click on the first line in the Network tab (professionals_add).
- Look at the "Headers" subtab. Look for where it says the POST method was used, and compare how this request is similar and different to the GET request you did earlier.
- Look at the "Payload" subtab. Compare the "name" attributes in the Form. Change them back and forth until you are comfortable with how they relate to what is logged on the "Payload" subtab.
- Look at the "Responses" subtab. See where the "message" comes into the HTML and how it appears in the HTML.
- Look at all the other line-items on the Network tab that were requested and compare them to the HTML response of the first line item, so you understand exactly why the server requested all of it.
- Now look at the flask application code. Take a moment to look at which route responds to the POST request. Trace it line by line.
- Take a moment to understand thoroughly how the POST data is received by the Flask code. Change variable names, print things out to the Flask console, look up Flask reference information on request.form and request.files, and/or anything else you need to satisfy yourself that you understand how the variables work, which names are important and which names are unimportant, how the message is passed to render_template and handled by the form.
Stage 3: Valid Form Submission (POST Request).
- Keep dev tools open.
- Submit the form with valid data.
- The form should render with no message, but the professional list should refresh with the new professional.
- Repeat as many times as needed to do the steps below, going to the data_restore route as often as desired to reset the data.
- Click on the first line in the Network tab (professionals_add).
- Look at all the other line-items on the Network tab that were requested and compare them to the HTML response of the first line item, so you understand exactly why the server requested all of it.
- Now look at the flask application code. Compare the execution line by line until you understand why this request called on the model function professionals_add() where the request with invalid data did not. Feel free to print things out to the Flask console or anything else you need to understand thoroughly.
- Now, look at model.py. Look at each line of the professionals_add() function. Use print to show intermediate values in this model function.
- Open sqlite3 on the company.db file to watch what happens to the table before and after adding.
- Pay special attention to the order of cur.execute(), cur.lastrowid, and db.commit(). The idea is that the uncommitted database transaction gives the id without creating the row, and the row is only created if the file save works.
- Trigger the application to fail to reach the db.commit line (make the file save invalid or skip the line altogether or anything of that nature) and check how the database behaves in sqlite3.