hatgift.blogg.se

Flask blueprint example
Flask blueprint example





  1. FLASK BLUEPRINT EXAMPLE HOW TO
  2. FLASK BLUEPRINT EXAMPLE FULL
  3. FLASK BLUEPRINT EXAMPLE CODE

This inheritance in the templates means there is some confusion over why the static folder doesnt work in the same way. So if you have a template in your lists blueprint called lists.html the Flask app will first look in the main static folder for lists.html and, if it does not find it, will then search in the Blueprints folder for the template. Template folders are created within Blueprints in the same way however, and I think this is where the confusion starts, template folders will search within blueprint template folders if the template is not found in the main static folder.

flask blueprint example

to the start of the static folder in order to use the static folder within the blueprint you are currently active in.Ī note about templates vs static folders. So the correct and simplest solution seems to be you do not include a static url path in the blueprint definition and when making a template you simply add the. Perhaps this approach might be useful if you have several blueprints which register at the root of the site but when using prefixes this approach seems pointless. Will now give you the path /lists/lists/static/css/custom.css because it is adding in the additional static url path as well as the blueprint prefix. Will result in the same issues as before with it looking in the main static folder for the app and not the blueprints static folder. If you add this static_url_path line you would again expect any templates or other content within the blueprint to work but if you are using templates then you still have to define the static location and using There are quite a few conversations around the internet on the use of the static_url_path variable that can be included in the blueprint definition like so The simplest solution is to add a location to the static folder by using either. They differ slightly from templates files which I will explain below in more detail. we just get the main static folder from the base blueprint at /static and of course, the file does not exist there. We might expect the URL to be /lists/static/css/custom.css So if we add a template into the lists template folder and use something like So in the nature of blueprints you would then expect the path to this folder to be /lists/static as we are defining a prefix and then defining a static folder location.

flask blueprint example

FLASK BLUEPRINT EXAMPLE HOW TO

What I wanted to discuss is how to make proper use of the static folder within the blueprint, as you can see above we define the static folder, to contain our css and js files for example, as being “static”

FLASK BLUEPRINT EXAMPLE FULL

This isn’t a full Blueprints tutorial and if you want to learn more about Blueprints then either go here, here or here you create a folder for example /lists/ and in there you define an _init_.py file or similar that will contain the instructions for this blueprint.

FLASK BLUEPRINT EXAMPLE CODE

You may do some includes to pull in additional functions but that is probably it.Īs your Flask application grows in size this is obviously not feasible and the different sections of code need to be separated out into different folders or separations of concerns as it were.įor this, you can use Blueprints. In most Flask tutorials you see throughout the web they will have a single file which contains everything including your routes, models and any other code.

flask blueprint example

In Flask you have a concept called Blueprints. Static folders don’t inherit as templates do.







Flask blueprint example