Creating a urls.py file for Flask, as in Django

Can someone help / explain me how to create urls.py file for Flask, like in Django?

  • main.py - the main project file. It includes only the application ( app.run() ).
  • urls.py is located in the same directory and should provide views from views.py .
+4
source share
2 answers

You can do this as described in the Flask documentation , basically by calling app.add_url_rule to set up routes, rather than using a decorator.

+5
source

In addition to the Flask documentation, this can be resolved as follows:

When creating the Flask application, upload the file 'urls.py'

 app.register_blueprint(apps.someapp.urls.mod) 

Then create urls.py as follows:

 from flask import Blueprint from apps.someapp.views import SomeView # set method as endpoint view = SomeView.as_view('someview') # Create the blueprint for this app mod = Blueprint("payment_methods", __name__, url_prefix="/someapp/", template_folder="templates") # Add the view as route; methods like GET, POST, PUT will automatically route to class methods with parameters mod.add_url_rule('<int:id>/', view_func=view) 
+1
source

Source: https://habr.com/ru/post/1446965/


All Articles