Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way, in Django, to define routes using Flask-style route syntax?

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?

like image 752
Ken Kinder Avatar asked Jul 06 '16 17:07

Ken Kinder


People also ask

How will you declare a route 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.

How do you make a route in Flask?

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.

How does URL routing work in Django?

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).

Which code block is used in Python Flask to map the URLs to a specific function that will handle the logic for that URL?

route("/") is a Python decorator that Flask provides to assign URLs in our app to functions easily.


1 Answers

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.

like image 144
alecxe Avatar answered Oct 29 '22 14:10

alecxe