Question

Remove href params from address bar.

Hi all, I’m using Flask to build my site, currently I have a navbar setup with links to my other pages, here’s one:

<li _class_="nav-item"> <a _class_="nav-link" _href_="{{ _url_for_('pictures',_filename_='templates/pictures.html') }}">Pictures</a> </li>

Now it links fine and works as I want it to, however it also adds the filename after the URL: http://127.0.0.1:5000/pictures?filename=templates/pictures.html. Is there any way to remove the filename, so the address bar simply reads http://127.0.0.1:5000/pictures

Any help would be appreciated.

(I’m self-taught when it comes to HTML and CSS, so forgive any obvious mistakes. if you need any more code, just ask)

Thanks


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
August 28, 2023

Heya,

To achieve this in Flask, you don’t typically pass the template name through the URL as you’re doing. Instead, you should separate the routing logic from the template rendering.

Here’s a simple refactoring of your situation:

  1. Flask Route

In your Flask app, you’d define a route for ‘pictures’ that will render the desired template:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/pictures')
def pictures():
    return render_template('pictures.html')

This sets up a route /pictures which will render the pictures.html template when accessed.

  1. Navbar Link

In your HTML (probably within a base template if you’re using one), your link to the pictures page would look like:

<li class="nav-item">
    <a class="nav-link" href="{{ url_for('pictures') }}">Pictures</a>
</li>

By doing this, the resulting link will be http://127.0.0.1:5000/pictures without any extra query parameters.

  1. General Tips
  • The Flask render_template function looks for templates in a folder named templates by default. So you typically don’t include templates/ in the template name.
  • url_for takes the name of the function (the view function in Flask terminology) as its argument, not the template filename. Hence url_for('pictures') corresponds to the pictures function in your Flask app.
  • Ensure you have a templates folder in the same directory as your Flask script, and that’s where you put your pictures.html file.
  • Always separate routing logic (which view function to execute) from rendering logic (which template to use) for cleaner and more maintainable code.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel