django-filter looking for templates only in venv - django-templates

Tried to make custom widget for RangeFilter. Every time django raises
TemplateDoesNotExist
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django\forms\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django_filters\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django\contrib\admin\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django\contrib\auth\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\debug_toolbar\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\bootstrap5\templates\MyRangeWidget.html (Source does not exist)
Every path django search for template is in venv. And when i put my widget to match this path, it working just fine.
venv\lib\site-packages\django_filters\templates\MyRangeWidget.html
But i believe its not correct way to do this.
My widget:
<div class="input-group">
{% for widget in widget.subwidgets %}
{% if forloop.first %}
<span class="input-group-text"><i class="bi bi-chevron-left"></i></span>
{% endif %}
{% if forloop.last %}
<span class="input-group-text"><i class="bi bi-chevron-right"></i></span>
{% endif %}
{% include widget.template_name %}
{% endfor %}
filters.py
import django_filters
from .models import *
from .widgets import MyRangeWidget
class LandFilter(django_filters.FilterSet):
price = django_filters.RangeFilter(widget=MyRangeWidget)
size = django_filters.RangeFilter(widget=MyRangeWidget)
class Meta:
model = PriceChangeHistory
fields = ['price', 'size']
widgets.py
import django_filters.widgets
class MyRangeWidget(django_filters.widgets.RangeWidget):
template_name = 'MyRangeWidget.html'
settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
INSTALLED_APPS = [
'django_filters',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'bootstrap5',
'search',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Just to mention, i tried different paths in template_name. Including even absolute path before i notice that django not even try to search for it anywhere except venv.
Have no idea if its me, or its django-filter package problem.

2nd day of searching way how to fix it, and right after i ask it here i found the way.
Added django.forms in installed apps and FORM_RENDERER variable like so:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.forms',
'django_filters',
'debug_toolbar',
'bootstrap5',
'search',
]
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
Now template_name in widget works as expected.

Related

Parametric access to DBT nested variables in dbt_project.yml

Based on Nested variables in dbt_project.yml file of dbt
vars:
env: env_var('DBT_ENVIRONMENT')
bucket:
dev: "dev-bucket"
uat: "uat-bucket"
ppd: "ppd-bucket"
prd: "prd-bucket"
I would like to access to the bucket name based on the value of the env var env.
For example I would reference the dev bucket like:
{{ var('bucket')['dev'] }}
But I want instead to call it parametric based on the var env with something similar:
{{ var('bucket')[ {{ var('env') }} ] }}
It's just that with this I get the following error message:
Compilation Error Could not render {{ var('bucket')[ {{ var('env') }} ] }}
and I don't know if this is possible to reference with DBT syntax.
Any suggestions on how to achieve this?
A possible solution can be
{{ var('bucket')[ env_var('DBT_ENVIRONMENT') ] }}
but still I'd prefer to reference directly var('env') from the dbt_project.yml, so open to better solutions.

Cannot see Theme App Extension in theme builder

Hi Have developed a very simply Theme App extension for Shopify but cannot see it in the Shopify theme builder.
Files are:
/extensions/myextension/blocks/app-block.liquid
Hello world
{% schema %}
{
"name": "Test",
"target": "section",
"templates": ["product", "index"],
"settings": [
]
}
{% endschema %}
/extensions/myextension/shopify.theme.extension.toml
name = "myextension"
type = "theme"
I then run npm run dev which runs as expected, I can also the extension under the Apps section in my Shopify admin, but cannot see any blocks for it in the theme editor.
Any guidance would be appreciated.
Quick troubleshooting guide:
Have you built and deployed your extension using CLI?
Did you enabled preview in partner panel? You can try setting up an initial version.
Is your theme 2.0 theme? You should be able to use your component on the homepage.

Airflow BigQueryInsertJobOperator configuration

I'm having some issue converting from the deprecated BigQueryOperator to BigQueryInsertJobOperator. I have the below task:
bq_extract = BigQueryInsertJobOperator(
dag="big_query_task,
task_id='bq_query',
gcp_conn_id='google_cloud_default',
params={'data': Utils().querycontext},
configuration={
"query": {"query": "{% include 'sql/bigquery.sql' %}", "useLegacySql": False,
"writeDisposition": "WRITE_TRUNCATE", "destinationTable": {"datasetId": bq_dataset}}
})
this line in my bigquery_extract.sql query is throwing the error:
{% for field in data.bq_fields %}
I want to use 'data' from params, which is calling a method, this method is reading from a .json file:
class Utils():
bucket = Variable.get('s3_bucket')
_qcontext = None
#property
def querycontext(self):
if self._qcontext is None:
self.load_querycontext()
return self._qcontext
def load_querycontext(self):
with open(path.join(conf.get("core", "dags"), 'traffic/bq_query.json')) as f:
self._qcontext = json.load(f)
the bq_query.json is this format, and I need to use the nested bq_fields list values:
{
"bq_fields": [
{ "name": "CONCAT(ID, '-', CAST(ID AS STRING), "alias": "new_id" },
{ "name": "TIMESTAMP(CAST(visitStartTime * 1000 AS INT64)", "alias": "new_timestamp" },
{ "name": "TO_JSON_STRING(hits.experiment)", "alias": "hit_experiment" }]
}
this file has a list which I want to use in the above mentioned query line, but its throwing this error:
jinja2.exceptions.UndefinedError: 'data' is undefined
There are two issues with your code.
First "params" is not a supported field in BigQueryInsertJobOperator. See this post where I post how to pass params to sql file when using BigQueryInsertJobOperator. How do you pass variables with BigQueryInsertJobOperator in Airflow
Second, if you happen to get an error that your file cannot be found, make sure you set the full path of your file. I have had to do this when migrating from local testing to the cloud, even though file is in same directory. You can set the path in the dag config with example below(replace path with your path):
with DAG(
...
template_searchpath = '/opt/airflow/dags',
...
) as dag:

How to use Markdownit modul with Nuxt

I am using Nuxt, Vuetify and Storyblok. I have problem when I want to display markdown from storyblok. I try to use Markdownit modul.
First I install it with npm, then import to modules and use it with v-html, but still don't get expected results.
What I am doing wrong?
npm install markdown-it --save
modules: [
['#nuxtjs/markdownit', {
html: true,
linkify: true,
breaks: true,
}],
['storyblok-nuxt', {
accessToken: process.env.NODE_ENV == "production" ? "ygL5rmck1lGa42Vaai7x1Qtt" : 'iyPj3vEKmPladyz3zeqKuwtt',
cacheProvider: 'memory'
}]
],
<p v-html="content"></p>
Result
**Bold text**
![Img](//a.storyblok.com/f/56157/700x699/2a66b0316d/photo.jpg)
You need to actually use the module, too. You're just importing it at the moment.
What I mean is to write
<p v-html="$md.render(content)"></p>
instead of
<p v-html="content"></p>

TemplateDoesNotExist at/

here is my site url http://webtrick.heliohost.org/
my template directory settings:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__) , 'templates').replace('\\','/')
)
view.py
from django.http import HttpResponse
from django.template import Template , Context
from django.shortcuts import render_to_response
def index(request):
return render_to_response('index.html')
url.py
from django.conf.urls.defaults import patterns, include, url
from webtrickster.views import index
urlpatterns = patterns('',
url(r'^$', index)
)
i don't know what's wrong with this code
any help appreciated
Make sure your Django app is in the INSTALLED_APPS in settings.py. That was my problem with it. So if you have the templates folder in your polls app folder, you need to add the 'polls' at the end of the installed apps in the settings file.
Add you application into setting.py end of the INSTALLED_APPS like below:
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
'mysite'
]
and your templates dir should be in mysite like
mysite
-settings.py
-templates/
I wasted a lot of time trying to figure out what was wrong with my 'templates' directory and found out that :
You may have forgotten to add TEMPLATE_DIR in the following segment of settings.py :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
So it should have 'DIRS': [TEMPLATE_DIR,], instead of 'DIRS': [],
Also see answers for : TemplateDoesNotExist at /
os.path.join(os.path.dirname(__file__) ,'../templates').replace('\\','/')
That worked for me.
First you need to make one folder named 'templates' in your django project folder then, you can add template directory by two ways open your settings file then add any of the following code
1) You can specify your template path directly by
TEMPLATE_DIRS = (
'D:/Django/web/Kindset/templates' #your file path ,this is my template directory path
)
2) Or if you want to use generic path means use
TEMPLATE_DIRS = (
'os.path.join(BASE_DIR, "templates"),'
)
You may forgot to install you app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tutorials'
]
Make sure you also add dirs properly in your templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
TEMPLATE_DIRS = (
"mysite/templates"
)
TemplateDoesNotExist - file exists, no permissions issue
My templates folder wasn't a python package because it was missing the __init__.py file.
This might have been the reason why Django was not locating my templates.
Many good answers here, but on updating an old project (nightmare=zinnia, django_cms, AUTH_USER_MODEL = 'accounts.CustomUser') that was not maintained for many moons from django 1.6 to django 1.7 as stage one of a very long process, I found nothing worked. Then I noticed that the errors had quotes around the template it was trying to load:
/home/vagrant/venv/crowd88/local/lib/python2.7/site-packages/djangocms_admin_style/templates/'core/includes/ga.html' (File does not exist)
So I looked at the source and found that quotes are stripped this way:
# lib/python2.7/site-packages/cms/utils/plugins.py
...
template = get_template(force_unicode(node.template).strip('"'))
...
Then I noticed that single quotes were being used in the loaders
{% include core/includes/ga.html' %}
So the fix was
{% include "core/includes/ga.html" %}
using a regex process
Move your "templates" folder to the main and not in any sub folder. Or on the same level as other projects folder. This might have been the reason why Django was not locating my templates.
My project worked after doing this.
make sure your dirs directory in settings.py looks like this
'DIRS': [os.path.join(BASE_DIR, 'templates')],
By default, inside the settings.py file, the 'DIRS' array is empty as you can see below.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
So to solve the issue, just add BASE_DIR / 'templates' to the array to look like below. And you are good to go.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Go to the settings.py file and check the TEMPLATES section. Notice the DIR value, by default it should be []. Add "os.path.join(BASE_DIR, 'templates')" inside this. It should look like this :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I encountered the same error. After checking TEMPLATE_DIR and INSTALLED_APPS a thousand times I noticed a small typo in one .html template:
{% extends "my_app/base.html " %}
should have been
{% extends "my_app/base.html" %}
(notice the white space in the first command)
If you have tried some of the solutions above and it not worked try checking whether the template is within the correct folder.
Template outside of the templates/appname folder:
appname/
|
|__templates/
| |
| |__appname/
| |__appname_form.html
|__appname_list.html <------ outside of the appname/ folder (WRONG)
Like this if you have a url that send to this template, Django will through a TemplateDoesNotExist since the template does not follow the path you placed in TEMPLATES.DIRS.
Try fixing like:
Template inside of the templates/appname folder:
appname/
|
|__templates/
| |
| |__appname/
| |__appname_form.html
| |__appname_list.html <------ inside of the appname/ folder (RIGHT)
Therefore, like this you may fix this exception.
if {% extends "blog/base.html" %} make sure base.html is in the blog folder and not in Templates or any other directory. That solved it for me.
This worked for me:
1.Make a folder in your app called static and create the css file in the static folder
2.Make another folder still in your app called templates and create the html file there
On the first line of your html file write this: {% load static %}
Link the css file inside the head tag as follows: href="{% static 'index.css' %}"
Thank me later.
Got the same problem !
I've already followed all solutions provided above but pfff ! I were lost and maybe someone could face it too!
So I solved it by only
return render(request, 'profile.html')
and not
return redirect(request, "profile.html")
Happy debugging !!!
After debugging a lot, i found out that the path which i was giving in views.py to render the template is wrong so it was showing TemplateDoesNotExist at/ error
for Example the mistake i was making is this
Wrong code :-
return render(request, 'show/index.html', {'product_objects': product_objects})
Right code :-
return render(request, 'shop/index.html', {'product_objects': product_objects})
the app name was shop not show.
Hope it will help someone
I think the answer is pretty simple just give the right path
Use this if you have your templates folder in your app
BASE_DIR = Path(__file__).resolve().parent.parent
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'DIRS': [BASE_DIR,"templates"],