Search code examples
pythonflaskjinja2

werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'articles' with values ['page']. Did you mean 'static' instead?


I have issues with a Jinja Template im trying to work on and need some advice, I keep gettign the error

werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'articles' with values ['page']. Did you mean 'static' instead?

here is the jinja template:

  <div class="flex justify-center mt-10">
                <ul>
                    <!-- previous page -->
                    {% if blogs.has_prev %}
                        <li>
                            <a class="text-white hover:text-gray-400"
                               href="{{ url_for('articles', page=blogs.prev_num) }}">Previous</a>
                        </li>
                    {% endif %}
                    <!-- all page numbers -->
                    {% for page_num in blogs.iter_pages() %}
                        {% if page_num %}
                            {% if page_num != blogs.page %}
                                <li>
                                    <a href="{{ url_for('articles', page=page_num) }}">{{ page_num }}</a>
                                </li>
                            {% else %}
                                <li class="active">
                                    <a class="text-white hover:text-gray-400" href="#">{{ page_num }}</a>
                                </li>
                            {% endif %}
                        {% else %}
                            <li>
                                <span class="ellipsis">…</span>
                            </li>
                        {% endif %}
                    {% endfor %}
                    <!-- next page -->
                    {% if blogs.has_next %}
                        <li>
                            <a class="text-white hover:text-gray-400"
                               href="{{ url_for('articles', page=blogs.next_num) }}">Next</a>
                        </li>
                    {% endif %}
                </ul>
                <!-- More posts... -->
            </div>

and here is the flask route im pulling from:

@blogs_blueprint.route("/articles", methods=["GET"])
@blogs_blueprint.route("/articles/<int:page>", methods=["GET"])
def articles(page=1):
   per_page = 6
   blog_list = Blog.query.order_by(desc(Blog.date_created)).paginate(page=page, per_page=per_page, error_out=False)
   current_year = datetime.now().year
   return render_template("articles.html", blogs=blog_list, year=current_year)
   ```

Solution

  • Usually an error like that occurs when the flask app doesn't know about that route.

    If you have a blueprint you have to remember to register it with your flask app

    from . import blog # assuming the blueprint is in blog.py
    app.register_blueprint(blog.blogs_blueprint)