Setting aside any strongly-held feelings about Django vs Flask, I have a whole bunch of Flask-style routes I'd like to convert to Django. They look like your usual Flask routes:
'/foo/<spam>/<int:eggs>/'
This gets even more complex with converters in Flask like path
:
'/foo/<path:location>'
So I have all of these routes, and I'd rather not try to figure out regular expressions that approximate them in converting my views. I'm looking to just parse the Flask-style routes in Django. Is there a way to use Flask-style route patterns in Django?
To create a route in Django, we will use the path() function that accepts two parameters: a URL and a View function. All the routes are created inside the urlpatterns list. Simply add a new path as below and a new route will be created.
App Routing means mapping the URLs to a specific function that will handle the logic for that URL. Modern web frameworks use more meaningful URLs to help users remember the URLs and make navigation simpler. Example: In our application, the URL (“/”) is associated with the root URL.
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).
route("/") is a Python decorator that Flask provides to assign URLs in our app to functions easily.
Have not personally used it, but this sounds exactly what you are asking about - django-fsu
:
Flask-Style URL Patterns for Django
I would though still try to stick to Django URL-routing style and avoid "transitional" solutions like this. You can also use django-fsu
temporarily to migrate, then cover all the endpoints with end-to-end and functional tests and, then, once you have the coverage, migrate to the Django native URL routing style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With