-1

My problem is I can hardcode the links but when I do it dynamically, I just get an error

@app.route('/posts/<string:id>')
def show_post(id):
    # Retrieve the post with the specified id
    # You can replace this with a database query to fetch the post
    database = db.child("contents").get()
    posts.clear()
    for title in database.each():
        posts.append(title.val())
    count = 1
    newposts = {

    }
    #nposts = posts
    for post in posts:
        newposts.update({str(count): post})
        count+=1
 
    newposts.get(id)
    sus = newposts.get(id)
    print(url_for('posts'))

    #postid = newposts.get(id)
    if newposts:
        # Return the post with the specified id  
        return f'<h1>{newposts[id]}</h1>'
    else:
        #Return a 404 error if the post with the specified id is not found
        return '<h1>404</h1><p>Post not found</p>', 404
    #return render_template('posts.html', title='Posts', postid = postid )



{% extends "layout.html" %}
{% block content %}
    <h1>Posts</h1>

    
    {% if posts is iterable %}
        {% for post in posts %}
            <article class="media content-section">
              <div class="media-body">
                <p class="article-content">{{ post.id }}</p>
                <h2><a class="article-title" href="posts/1">{{ post.title | safe }}</a></h2>
                

                <p class="article-content">{{ post.content | safe }}</p>
                <p class="article-select">{{ post.select | safe }}</p>
                <!--<p class="article-select">{{ post.img | safe }}</p>
                <img src="{{ post.img }}" alt="image alt text" />       --> 
                <img src="{{ url_for('static', filename=img) }}">


                
                

              </div>
            </article>
        {% endfor %}
    {% endif %}
{% endblock content %}


This is the code without the dynamic coding when i use this link in html in html I get type error TypeError: Flask.url_for() takes 2 positional arguments but 3 were given

                <h2><a class="article-title" href="{{ url_for('posts','post.id') }}">{{ post.title | safe }}</a></h2>

I tried to use dynamic links but I was given errors

Borki
  • 1
  • 2

1 Answers1

0

The solution is simple, which is to give the url_url() the name of route function

its will be url_for("show_post") then you will have to give the paramters value you want of show_post route function

will be like this

url_for("show_post",id=<your value>) 

I recommend you to review this link https://stackoverflow.com/a/35936261/18420610