Trouble extending Flask-Security login template - flask

I'm using Flask-Security in my project and I would like to extend templates (namely login) it offers, in order to style them with custom CSS. Following the documentation, I've changed the SECURITY_LOGIN_USER_TEMPLATE config value to point to my template. In this template I'm extending the template provided by Flask-Security ("security/login_user.html") and changing the content of some of the blocks it defines (or its base).
My template renders as expected (looks like the standard "security/login_user.html"), the only problem being is that the blocks I'm trying to overwrite are not overwritten.
My template looks like this: login.html:
{% extends "security/login_user.html" %}
{% block content %}
<p>There should be no form.</p>
{% endblock content %}
To confirm that I'm not not rendering the original template I've tried changing contents of the template file to empty and it works as expected - nothing is rendered.
What am I missing?

After some time with debugger, I realized that something is wrong with the package Flask-Security that is available on PyPi. When developing my project I relied on source code that is present on their official github page. If you take a look at the login template, you notice that it indeed extends base template which provides definition of the blocks you can later overwrite.
However when you download the package from PyPi and then check the template you notice that it differs - it does not use Jinja2 inheritance, nor does it use any blocks, therefore the problem described in the question.
In other words, PyPi package is reflecting latest stable release (as noted by nick-k9), which differs significantly from develop branch.

Related

why my django groundwork doesn't work

I install django-groundwork by pip and set add 'groundwork' to the my django app but it seems doesn't work,I don't know why?When I use command 'python manage.py help' to check which command could be used ,there is no 'ground' but the module dose exist.Please give me some help
The groundwork pagkage doesn't provide any management commands. It only provides some templates and template tags. That's why you're not seeing anything when calling manage.py (or django-admin.py'
The docs say it clearly, but you can check out the code... there's simply no management/commands directory inside the installed package.
Groundwork is a small set of template tags and filters that simplifies
working with Zurb Foundation
To truly say that the application "doesn't work", you'd have to use the template tags it provides.
Do this in a template
{% load groundwork_tags %}
Then do this
{% groundwork_icon 'info' '5em' 'text-centered' %}
If that breaks your template, it means the app isn't properly installed, is broken, or otherwise there's something else wrong.
Read about django management commands a little, it will all be clear (make sure to read about the version of django that you have installed.. the link will always point to the development version so things might be different there):
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

Phalcon Design approach / pattern

Hello everyone and thank you for taking time to read this.
I've been using Phalcon for quite a while for a high performance JSON/XML API.
The backend managing this application was/is still driven by an oudated version of symfony, but it is gonna be dropped in favour for Phalcon and the Volt Template engine.
Now my problem is the following:
Imagine a base application and a basic template and the application is modularized. Most modules are gonna be developed by different teams but they all have to integrate niceley, which from the program logic side is not a problem.
But imagine the following:
You have a simple page, some forms, head, navigation, etc, etc.
Now someone wants to add a module which injects a template block into the footer for whatever purpose. For example adding a TagCloud (for SEO purposes) into the footer.
The idea here is, that the plugin has way to edit any template files other than the ones it brings itself.
How can this be achieved without having to change the base templates after the initial development?
The idea is basically to hook into a event, lets call it TEMPLATE_RENDER for simplicity.
TEMPLATE_RENDER is fired, every listener that is registered for it now has its chance to add stuff to the template like additional blocks etc. All without having to manually change the core templates.
It would be sufficient if there is a way to simply add a bunch of template files together in Volt and output the compiled result.
EDIT:
Okay, after some thought what I'm looking for in Volt is this:
Compiler#compileMultipleFiles(String... files);
So it can be used like this:
$compiler->compileMultipleFiles('/path/to/template1','/path/to/template2', ...);
Which would do nothing else "in theory" than take everything in file1, file2, ..., fileN and put it into one large file and then compile that as a single template. If it is not yet possible I could emulate that function by simple having each files contents combined into a single file or cache variable and use compileString() but that would break any relative paths in the template, which would be a problem.
I could also compile each template down manually, but than I would end up with an pure html document without the ability to append to blocks in the main template.
Apparently there is no such function directly.
You can however use an array and iterate over this area at the end of the primary template and dynamically include any file passed into there.
I believe that you're looking for a Volt include. You can leave some tests in your templates like:
{% if foo.enabled %}
{% include "foo/bar.volt" %}
{% endif %}
If you need something more complex than this you can use template inheritance also.

Template Partials in Symfony 2

Are there such things as partials in Symfony 2, reusable templates from anywhere, effectively?
I have found include http://twig.sensiolabs.org/doc/tags/include.html but this only allows the rendering of a template in a specific directory structure.
What I really want a folder that contains all my partial templates, rather than lumping them into my main views?
So I want to be able to do something like
{% include "Bundle:Default:Partials:view.html.twig" %}
Update
I do not want to use the enforced structure of Bundle:Controller:Template structure. I do not want to use this as it means putting all my template partials in with my main view templates. I need something that lets me do Bundle:Controller:PartialDir:Template
You can already do that. The symfony2 docs has a section describing how to do this.
http://symfony.com/doc/current/book/templating.html#including-other-templates
In Symfony 2.4 (the version I am currently using but it probably works in other 2.x versions as well) you can do the following:
{% include '::_partials/partial.html.twig' %}
or in Symfony 2.2+ using the include function
{{ include('::_partials/partial.html.twig') }}
This will look for the partial.html.twig template inside of the app/Resources/views/_partials directory. You can obviously name the _partials directory whatever you want. It also works without the '::' prefix.
I know this is old, but the way to achieve what OP asks is the following:
Rather than doing
bundle:controler:partialDir:template
we have to switch it slightly to achieve:
{% include 'Bundle:PartialDir/Controller:Template' %}
To include the controller, you'll need to refer to it using the standard string syntax for controllers (i.e. bundle:controller:action):
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}

Cannot Extend Django 1.2.1 Admin Template

I am attempting to override/extend the header for the Django admin in version 1.2.1. However when I try to extend the admin template and simply change what I need documented here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template), I run into a recursion problem.
I have an index.html file in my project's templates/admin/ directory that starts with
{% extends "admin/index.html" %}
But it seems that this is referencing the local index file (a.k.a. itself) rather than the default Django copy. I want to extend the default Django template and simply change a few blocks. When I try this file, I get a recursion depth error.
How can I extend parts of the admin? Thanks.
SOLUTION: Rather than extending, I copied the files into my_templates_directory/admin/ and just edited them as I wished. This solution was acceptable, though not ideal.
The contrib/admin/templates/admin path needs to go before the directory with your custom admin templates in your list of paths in TEMPLATE_DIRS in your settings.py
Create a symlink to contrib/admin/templates/admin/ in your templates directory and use it in your {% extends %} statement.
cd /path/to/project/templates/
ln -s /path/to/django/contrib/admin/templates/admin/ django_admin
Now in your admin/index.html use {% extends "django_admin/index.html" %}
EDIT: Just realized that you're on windows... Not sure how to achieve the same results. Hopefully this still helps the folks on linux.
SOLUTION: Rather than extending, I copied the files into my_templates_directory/admin/ and just edited them as I wished. This solution was acceptable, though not ideal.

How to make project templates and Satchmo templates co-exist?

I'm working with a Satchmo installation that resides within an existing project. This project has its own templates as well as templates for some of the various apps that are installed. Some of these app-specific templates have their own app_base.html variations that expect to derive form base.html. I'd like to be able to do the same thing with my Satchmo templates and have them reside within my project's base, but also have some additional html added around all of them.
/templates
base.html
index.html
/news
news_base.html (extends base.html and adds news-specific template features)
index.html
detail.html
/store
base.html (overriding Satchmo's base)
This structure works somewhat, but not how I expected. in /store/base.html (Satchmo's base) I've simply replaced everything with a test message. I can see the message, so I know satchmo is loading its base and not the site's base. However, I can't extend my project's base anymore since using:
{% extends "base.html %}
Yields a recursion error since its calling itself and the following simply won't work.
{% extends "../base.html" %}
I realize that I can change my project's base.html to a slightly different name and point all app-specific templates at them, but it seems like a pretty major hack on such a fundamental aspect of the template structure.
Hmm, I didn't think django looked up templates relatively like that.
Kinda crazy hack, but this should work:
/templates/store/base.html extends "global_base.html"
/templates/global_base.html extends "base.html"
Depending on how you have your template structure set up, it might also be a good idea to play with the settings.TEMPLATE_LOADERS variable.
TEMPLATE_LOADERS Default:
('django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source')
A tuple of callables (as strings) that
know how to import templates from
various sources. See The Django
template language: For Python
programmers.
For more information on how this affects the template loading process:
http://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
From the way you describe your problem, it seems like by commenting out the "app_directories.load_template_source" file line, you might be able to better find a way to accomplish what you're doing.
django.template.loaders.app_directories.load_template_source
Loads templates from Django apps on
the filesystem. For each app in
INSTALLED_APPS, the loader looks for a
templates subdirectory. If the
directory exists, Django looks for
templates in there.
This means you can store templates
with your individual apps. This also
makes it easy to distribute Django
apps with default templates.
For example, for this setting:
INSTALLED_APPS = ('myproject.polls',
'myproject.music') ...then
get_template('foo.html') will look for
templates in these directories, in
this order:
/path/to/myproject/polls/templates/foo.html
/path/to/myproject/music/templates/foo.html
Note that the loader performs an
optimization when it is first
imported: It caches a list of which
INSTALLED_APPS packages have a
templates subdirectory.
This loader is enabled by default.
I just had this same problem. It looks like the satchmo developers planned for this by putting an "empty" base in the shop template directory. While this may not be relevant to you anymore, I would have like to have seen this here.
You can make a "shop" directory in your template directory and copy the main satchmo base.html to that directory.
This worked for me.