Purpose: You learned the basics of Flask, its routes and Jinja templates, in Exercise 2.4A. It may not be obvious, but you can take any static webpage and transform it to Flask without changing ANY of the HTML or JS content. You will need to change filenames, reorganize the directories, and create an app.py. You are not to change any HTML, CSS, or JS content at all. Instead, you will rearrange app.py so links, images, CSS, JS, and AJAX work just as they did in Project Part 1. Please resist the temptation to change a single line of the HTML or JS itself until have your page fully working. See below for further explanation why we are doing this.
You may do this step on your local computer except for the part that directs you to use the webhost.
Move the content pages as follows, then set up the corresponding routes in app.py using @app.route(), without changing your HTML, links, images, AJAX include structure, or anything else in your HTML or JS:
| Current Filename | New File Name | Route in app.py |
| index.html | templates/index.html | / |
| professionals/index.html | templates/professionals.html | /professionals |
| services/index.html | templates/services.html | /services |
| contact/index.html | templates/contact.html | /contact |
| common/* | templates/common/* | /common/<path:pathname> |
| css/* (if used) | static/css/* | /static/css/<path:pathname> |
| js/* (if used, unless js needs templating) | static/js/* | /static/js/<path:pathname> |
| images/* (if used) | static/images/* | /static/images/<path:pathname> |
| <other static files> | static/<other static files> |
Check for broken links, images, CSS, JS, and AJAX that are routing to non-existent places. Resist the temptation to changing the links, images, CSS, JS, or AJAX in the HTML or JS. Instead, change app.py to listen for the correct URL. Left to your own devices, you might not think to migrate in this way, but doing so forces you to understand URLs, routing, and file paths better. It also ends up making the migration easier in the end overall.
You may run into a need to send a file that is not in the templates directory. Since render_template will not do this conveniently, it may be helpful to know that Flask has a function called send_file(filename) which simply reads a file into a string and returns it. Unlike render_template, send_file treats the relative path as starting from the project root, not the templates directory.
There is an issue with trailing slashes and relative paths that is peculiar to our webhost setup, that you will need to control for in Project Part 2. Create a small Python app to experience it. These instructions will assume your subdomain is "student3.cs3660.xyz." Please substitute in your actual subdomain.
Go to the webhost and setup a Python App called "slashtest." If you are student3, set up the URL as student3.cs3660.xyz/slashtest (substitute your subdomain). Edit app.py and put in a simple Flask app with the following two routes:
Now, first test the route with a trailing slash by going to "student3.cs3660.xyz/slashtest/". When you click on the link, it should go to the URL "student3.cs3660.xyz/slashtest/somewhere" and deliver the appropriate route. Not bad.
Now, test the route without a trailing slash by going to "student3.cs3660.xyz/slashtest" (no slash at the end) and click on the link. You should find that the link tries to go to the URL "student3.cs3660.xyz/somewhere" and gives you a 404 error. Bad.What's happening here is that the Browser can't tell whether "slashtest" is a file or a directory, and that affects how it makes relative paths when it renders links from HTML. Typically, general web servers control for this by discovering when something is a directory and automatically sending a redirect to the Browser with a trailing slash. And our webhost's does. But when we use Setup Python App, Phusion Passenger interrupts this behavior. So that makes it so we have to deal with the trailing slash problem.
There are many good strategies to control for the trailing slash problem. We want a solution that allows your code to work both on localhost and webhost without changing app.py. We also want a solution that allows us to use relative paths in our links so we can deploy anywhere. We will control for it by changing the "/" route redirect to a "home" subdirectory, using url_for to absolutize the redirect URL. This can be done entirely in app.py, a follows:
Please set begin setting up your Project Part 2 code with an app.py that contains this code. FYI, we will need this redirect only in Project Parts 2, 3, 4, and 4.5. In Project Part 5, we will stop needing it. You may start on localhost. If you deploy to webhost before finishing these exercises, please look at the Project Part 2 description and make the suggested modifications to .gitignore to avoid unexpected Browser behaviors.
By the end of this step, your project should work, delivering the same content as Project Part 1, and should be ready to control for the trailing slash problem when deployed to the webhost.