0

I try to dynamically generate my url_for like this and it is not working...:

search.html

<a href="{{ url_for('{{ country }}') }}"

This is where I query my data for my link from my database.

routes.py

from app import app, db
from app.models import 


@app.route('/search')
def search():
    country = Country.query.get(3)
    return render_template('search.html', country=country.country)

#this is one of the final page where the link in the search results lead
#I will have /portugal, /france, etc...
    @app.route('/germany')
    def germany():
        return render_template('germany.html')

and this is a clip of the information in my database:

models.py

from app import db

class Country(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    country = db.Column(db.String(120), index=True, unique=True)
    html_page = db.Column(db.String(255), index=True, unique=True)

    def __repr__(self):
        return '<Country {}>'.format(self.country)

Do I even need to store the URL since it is the same as the country name aka @germany

Alex Fortin
  • 101
  • 6
  • Check https://stackoverflow.com/questions/35107885/how-to-generate-dynamic-urls-in-flask – rkj Feb 05 '20 at 16:28
  • @RokJaklič I see his answer but I still don't understand how I generate the link to go to germany.html from the page search.html – Alex Fortin Feb 05 '20 at 16:47
  • So basically, im trying to figure out how to make the database and flask work together. should I store {{ url_for("germany") }} in my database or I should store www.mysite.com/germany for the country germany but I've been told this is bad if I change my site layout id have to update every entry in my database – Alex Fortin Feb 05 '20 at 17:15
  • I added my model.py file for more clarity – Alex Fortin Feb 05 '20 at 17:20

1 Answers1

0

If you already have a base template file setup for your countries then typically you want to generate that template with the specific information. Then typically you want to have your function definition setup something like this

# renders the given country page from the base template
# EX: if given "Germany" the resulting url will be:
# mywebsite.com/country/Germany
@app.route("country/<str:country>", methods=["GET"])
def render_country(country):
    # your specific code here
    return render_template("countrybase.html", country=country)

While your html link would look something like this:

<a href="{{ url_for('routes.render_country, country="Germany"') }}">Link to Germany page</a>

If you don't already have a base html template setup I would recommend researching Jinja templates and how Flask uses them. The example project from the official documentation has a great guide on how to get started.

But as a quick example your countrybase.html might look like this:

{% extends base.html %}

{% block header %}
    <h1>{% block title %}{{ country["name"] }}{% endblock %}</h1>
{% endblock %}
{% block content %}
   <div class="my-custom-css-class">
   {{ country["other_country_data"] }}
   <!-- etc.. -->
   </div>
{% endblock %}
Taylor Cochran
  • 439
  • 4
  • 16
  • Thanks for that. So really, I don't want to create on the fly all the countries html page. I want them to be hardcoded for SEO purposes so Google can index everyone of my pages. What id like tho, is to have a "search results" page that people would see a small snippets of that country as well as other countries nearby and each snippets would have a link to their respective hardcoded HTML page. – Alex Fortin Feb 05 '20 at 19:07
  • @AlexFortin I'm not particularly versed in SEO so I can't speak to that all too well. However there is a common principle between my earlier response and your search result problem. The main difference would be the addition of a _Jinja for loop_ in the middle of your content block. This structure allows you to dynamically add html tags and the content they contain from any iterable. Keep in mind you will also need to pass the collection of country results to the render call as well. – Taylor Cochran Feb 06 '20 at 01:38