Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use with dynamic routes ? #4

Closed
NiTRoeSE opened this issue Dec 7, 2022 · 3 comments
Closed

How to use with dynamic routes ? #4

NiTRoeSE opened this issue Dec 7, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@NiTRoeSE
Copy link

NiTRoeSE commented Dec 7, 2022

Hi,

i want to use flask-sitemapper for static and some dynamic routes together with blueprints.
I defined a instance of sitemapper in a separate file like describe in documentation and import this instance in every blueprint.
In mainfile i initialize the instance with "app" after i registered the blueprints.

This works well for all static routes like e.g. "/", "/about" etc.
But it failed for all dynamic routes like:

@blueprint_galleries.route("/gallery/<string:slug>", methods=['GET'])
def gallery(slug):
    id, meta = get_gallery_id_and_meta_with_slug("galleries",slug)
    if id != False and meta != False:
        photo_details = get_gallery_photos("gallery",id)
        return render_template("gallery.html", photos=photo_details, metadata=meta)
    else:
        return render_template("404.html"), 404

Error Message from werkzeug:

werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'blueprint_galleries.gallery'. Did you forget to specify values ['slug']?

I have no idea how i can solve this or use flask-sitemapper with dynamic routes.
I don't found anything about it in documentation.

Any help or hints would be great.
Thanks in advanced!

@NiTRoeSE NiTRoeSE changed the title How to use with dynamic routes How to use with dynamic routes ? Dec 7, 2022
@h-janes
Copy link
Owner

h-janes commented Dec 7, 2022

Hi,
Thanks for creating this important issue.

I have not given much thought to dynamic routes so far in flask-sitemapper, which should definitely change!

I am not sure if it is currently possible to use flask-sitemapper with dynamic routes - I will look into this and let you know - and add better support for this.

What is your expected behaviour for adding a dynamic route to the sitemap? Do you intend to provide a list of values for the URL variable(s) (slug in your example), and have a sitemap entry for each value? Perhaps something like:

@sitemapper.include(slug=["a", "b", "c", "d"])
@blueprint_galleries.route("/gallery/<string:slug>", methods=['GET'])
def gallery(slug):
    ...

Which lists the following URLs in the sitemap:

.../gallery/a
.../gallery/b
.../gallery/c
.../gallery/d

Or something else?

Thanks

@h-janes h-janes added good first issue Good for newcomers question labels Dec 7, 2022
@h-janes h-janes self-assigned this Dec 7, 2022
@NiTRoeSE
Copy link
Author

NiTRoeSE commented Dec 8, 2022

Hi, thanks for fast reply.

Mhhh i´m not sure what's really the best choice to do it, but lists seems to be fine.
No matter how you spin it, ultimately you have to somehow tell the sitemapper about the existing dynamic pages. So I think that lists are not the worst way to do that.
So the user can at least run some functions in his code that returns all existing dynamic pages as a list and then pass it to sitemapper like you described in your post.
I can't think of a better way to do it more elegantly.

Maybe as alternativ a additional "append to sitemapp" function would also work. So the user can do something like this...


pages = get_dynamic_pages()
for page in pages:
   sitemapper.add(page)

In this both contexts some kind of caching the sitemap would be great.

Kind regards.

@h-janes
Copy link
Owner

h-janes commented Dec 8, 2022

I have made some changes on the main branch: flask-sitemapper now supports dynamic routes as explained here, and now caches the sitemap's XML when it is first generated.

  • These changes are in sitemapper.py and url.py
  • A test has also been added for this in tests/test_dynamic_routes.py

Feel free to let me know what you think of this approach and if it works for you! These changes will be included in the next release (probably 1.6.0)

Thank you!

@h-janes h-janes closed this as completed Dec 20, 2022
@h-janes h-janes added enhancement New feature or request and removed question labels Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants