What's the difference between a blueprint and a template in Flask? - python

I'm self-learning web development, and I was confused about how blueprint and templates were different.
Blueprints support "common patterns within an application or across applications" - http://flask.pocoo.org/docs/1.0/blueprints/ but I don't see why templates can't suffice
Also, are these standard web terminology or just Flask specific?

Blueprint:
A Blueprint object works similarly to a Flask application object, but it is not actually an application. Rather it is a blueprint of how to construct or extend an application. It is particularly used for scaling large projects.
Blueprints are sets of applications in a application.
Blueprints may render templates to display the requested data.
Template:
Templates are used for rendering HTML document which are used to display in users browser. Templates file can be either static content or placeholder for dynamic data.
Templates are rendered by blueprint or an app which they pass data to templates.

Related

Serve react build as a template in flask

I want to serve a react project with Flask.
I need the index HTML to be served as a flask template so I can pass in a variable from Flask,
and the other static files (css, js, favicons) as normal static files.
How can I achieve that?
Edit: I just had the html sent as a flask template and put the static files into a folder which I declared as the static dir in flask.
As far as I know, you can not do that. Flask uses template htmls that are rendered on the server. Now if you ask whether can you code a react renderer for server-side rendering with Python/flask?, might be possible, I guess. But people built big companies around this idea, for example Vercel. So better team up. What you can do though you can make flask and react communicate through different ports. i.e. code your Flask back-end as a rest-api and build the react front-end.

Is there any way to use Django templates in Webapp2 framework in Google App Engine

I am using Django 1.2 template tags over webapp framework in Google App engine. I intend to migrate over to Webapp2. I was looking for a way to this in webapp2, but did not find a template engine for webapp2. So, should I continue with webapp's template engine or use pure Django template engine.
Many people use Jinja templates with webapp2 in GAE, but you can also use Django templates. The two template systems are very similar so it is fairly easy to switch between the two.
The template system that you use is quite independent of webapp2. It works like this:
Render your template to get a string representation of your HTML page
Transmit the string with webapp2
Feel free to use Jinja, Django, or any other templating system. Webapp2 doesn't provide templates because it is not part of its job.

Custom django admin template for app

I would like to create a custom index.html derived from the admin/index.html individual for each app in my django project.
For instance my folder structure is like:
app1
templates
index.html (different from the global template admin/index.html)
app2
templates
admin
base.html
index.html (global template index.html)
How can I achieve custom admin index.html files for my apps, that are recognized by django? For the moment only the index.html in the global template/admin folder is considered for rendering the index pages in my backend.
I'm using django 1.6
Unfortunately, only certain parts of the Django admin site can be overridden on a per-app basis, as it says in the documentation:
Not every template in contrib/admin/templates/admin may be overridden per app or per model. The following can:
app_index.html
change_form.html
change_list.html
delete_confirmation.html
object_history.html
Remember that the admin interface is itself and app, so it's going to do a single template sweep and load the first set of templates that comes up.
I think your two best bets are either to use multiple admin sites in your project or to add a custom view for specific apps -- the former is probably easier, but will be a problem if you don't want people to have to login separately to control certain things.

Flask-Admin (python) - Having trouble overriding templates

I am attempting to utilize Flask-Admin for an administrative interface to my web service.
I have it working, but the theme does not match what the rest of my site uses. The documentation here suggests that it is as simple as overriding the master template, but when I do that I end up with circular reference errors.
I have also tried on individual templates by copying the templates from the install directory to my application structure, but I cannot figure out the path they use. It is like it just defaults to the install directory, even if I have templates of the same name local to my flask app. From the docs: "You can override any used template in your Flask application by creating template with same name and relative path in your main templates directory."... yet I am not able to do that. Does it still expect admin/ in front of the templates?
Does anyone have an example? I basically need to override the bootstrap theme used, but some other customization could be nice. I'm new to flask, and python for that matter, so this may be quite simple...
You will still need to place your templates in the admin sub-folder of templates:
yourapp/
app.py
templates/
master.html # <-- This will not override admin/master
admin/
master.html # <-- This one, however, will :-)

How do I create site homepage in Django that integrates multiple apps?

So I have been following along with the Django Tutorial and have successfully created multiple "apps" that I now want to start integrating into a holistic website (which in Django seems to be called a project).
So here are my questions:
How do I create a site homepage that is mostly static data (HTML, CSS, and images), but also includes data from some of the models of my projects?
How do I link from this homepage to my apps? So if I have an app called "polls" (as in the demo), would linking to the polls page be as simple as linking to /polls?
I think the general approach is that you also have to add one app which glues all your other apps together. So if you need a special homepage which somehow has to have full or part access to all the other apps you create an app for it and point for example your root url to this app.
Following that (and depending if your other apps share data with each other or not) its really as simple as you said. The polls app could be accessible under /polls as an example, depending on how you configured it in your urlconf etc.

Categories