How could I implement a common template to be included in two Flask blueprints?
I am trying to create the following structure in my application:
app
+-- bp
| +-- hello
| | +-- templates
| | | +-- hello
| | | +-- hello.html
| | +-- routes.py
| +-- world
| + etc
+-- templates
+-- common_incl.html
in hello.html, I try to include another file by {% include '/templates/common_incl.html' %} but I always get a TemplateNotFound exception.
I would not like to expose the common_incl.html in static folder, as it isn't static but has Jinja variables expressions.
Re-reading https://flask.palletsprojects.com/en/2.0.x/blueprints/#templates might provide some guidance.
Depending on the nature of your application (and your blueprints) either put the base templates in the application's templates folder, or use a third blueprint that does nothing but hold common templates for your two templates.
Related
I have several custom settings I'd like to define in my Django app. Up to this point, I've put them in a constants.py file in each individual app.
myapp/constants.py
HOURS_PER_EVENT = 4
MAX_MEMBERS_PER_EVENTS = 150
MAX_EVENTS_PER_YEAR = 10
...
It just occurred to me I may be able to put these in settings.py and after a quick test everything seems to work fine. Is this allowed or is settings.py reserved for core django settings defined here? If it's not allowed, is there a better place to put these.
Yes it is allowed to extend your application settings, and it is really simple to do it. All that you need to do is a simple manipulation from your current project settings.py to a module settings.
Your file structure is probably like the following:
mysite/
|-- mysite/
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| +-- wsgi.py
+-- manage.py
What you need to do is to change it to something like this:
mysite/
|-- mysite/
| |-- __init__.py
| |-- settings/
| | |-- __init__.py
| | |-- base.py <-- (this is your old settings.py)
| | +-- constants.py
| |-- urls.py
| +-- wsgi.py
+-- manage.py
This is a pattern described in this tutorial.
I built a python 2.7 application with the below directory structure for my relevant files. How do cal methods located in different folder locations?
Data-Wrangling-OpenStreetMap-data
|
+---- process_data
| |
| +---- __init__.py
| |
| +---- data_cleaner.py
|
+---- main_code.py
|
+---- audit _data
| |
| +---- __init__.py
| |
| +---- audit_file.py
I have succeeded in doing it correctly for one class referenced from main_code.py via the use of:
from process_data.data_cleaner import DataCleaner
However, if attempt a similar pattern for another class located in seperate folder referenced by main_code.py for via the use of the import statement
from audit_data.audit_file import AuditFile
I get the error:
ImportError: No module named audit_data.audit_file
Any ideas as to what I may be overlooking and/or guidance on what further details I need to post to help find the solution to my problem?
from process_data.data_cleaner import data_cleaner
as data_cleaner is the file name data_cleaner.py and the second one data_cleaner is a class defined in it.
The cause of my problem was a silly one;
The folder containing the class I was calling was named audit _file whilst the folder I was calling within my code was audit_file
What didnt work
from audit_data.audit_file import AuditFile
What worked
from audit _data.audit_file import AuditFile
For those reading this watch out for unintended spaces in your folder names
I have a django app TestApp. In the templates directory of TestApp, I have an test_template.html. TestApp's settings.py has a reusable app (in the INSTALLED_APPS list) I created called reusable_app. There is a template called reusable_template.html in the templates directory of this reusable app.
Is it possible to include reusable_template.html in the test_template.html using the {% include} tag?
I tried {% include "reusable_app/templates/reusable_template.html" %} in test_template.html but got a TemplateDoesNotExist exception.
I also tried:
from django.template.loader import get_template
get_template('reusable_app/templates/reusable_template.html')
Django will look inside the app's templates directory, so you shouldn't specify this in the path. This should work:
{% include "reusable_template.html" %}
This assumes of course that the only app with a template called reusable_template.html is your reusable_app.
To minimise the risk of template collisions it is common to have a subdirectory in your app's templates directory, so that the path to the template is reusableapp/templates/reusableapp/reusable_template.html. You would then include it with:
{% include "reuseable_app/reusable_template.html" %}
If you have a project structure:
+app
|
+project/urls.py
| |
| +wsgi.py
| |
| +settings.py
|
+templates/base.html
|
+static/
then your template has to be in
+app+
| |
| +templates/your_app_base.html
| |
| +your_tempplate.html
+project/urls.py
| |
| +wsgi.py
| |
| +settings.py
|
+templates/base.html
|
+static/
Django first checks your apps's templates directory and if does not find templates, takes it from the main template directory. Make sure your template loaders are configured in settings.py
There is a lot of questions about finding templates, but I can't find anything related to custom tags.
I have following schematic project structure:
/
|- app1
| |- templatetags
| | |- my_helpers.py
| |- templates
| |- my_helpers
| |- my_tag.html
|- app2
|- templates
|- include
| |- inclusion.html
|- base.html
In my_helpers.py a simple inclusion tag is defined:
from django import template
register = template.Library()
#register.inclusion_tag('my_helpers/my_tag.html')
def my_tag(...):
...
app2/templates/base.html looks like this:
{% load my_helpers %}
... some markup ...
{% my_tag %}
{% include 'include/inclusion.html' %}
And here my_tag works just fine. But when I'm trying to use it also inside 'include/inclusion.html' (I've added a {% load my_helpers %} tag there), it fails with this:
django.template.base.TemplateDoesNotExist
TemplateDoesNotExist: my_helpers/my_tag.html
As I understand, it looks for templates only in current app. But why it happens only for included templates? And is it possible to make it work?
To achieve this you can take advantage of Django template loader's feature.
You can create your project structure like below--
|- templates
| |- common_templates
| |- my_tag.html
|- app1
| |- templatetags
| | |- my_helpers.py
| |- templates
| |- my_helpers
|
|- app2
|- templates
|- include
| |- inclusion.html
|- base.html
and your my_helpers.py can be like
from django import template
register = template.Library()
#register.inclusion_tag('common_templates/my_tag.html')
def my_tag(...):
...
since Django 1.4 (I think) django create a folder for my project when I start a project. Django add a folder for any application I created (with python manage.py startapp) at the same level of my project folder.
Project_name
|---project_name_dir/
|---application_dir/
`---manage.py
I really like the following folder structure:
Project_name
|---project_name_dir/
| |---application_dir/
| | |-- __init__.py
| | |-- models.py
| | |-- tests.py
| | `-- views.py
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
| |---templates/
| | `---application_dir/
| `---static/
| |---css/
| |---font/
| |---img/
| `---js/
|---deployment/
|---documentation/
|---config/
`---manage.py
Because I have a folder with all my django files (project_name_dir/) and other directories for non django files.
So why Django put application at the same level of my project folder?
In Django, the position of the application directory is not considered. Django only uses the name of the application.
Thus, the position of the application is basically a matter of convenience of the programmer.
This is also the reason why two apps should not have the same name: even if they are imported in INSTALLED_APPS as
('app.app1', 'app1')
Django only concerns with the last part after the dot, i.e. app1.
So, in the end, you can use the directory structure you want, as long as the apps' names don't collide and you point to the app on INSTALLED_APPS. Because of this, if there isn't any special reason, you should put them on the project's root, like Django does.