write form fieldsets in Python3.7 - python-3.x

I have updated my project from Python 2.7 and Django 1.8 to Python 3.7 and Django 3.0
Where BetterModelForm and fieldsets are suuported in Python 2.7
My first question is that can i use fieldsets in Python 3.7 ? If yes how can i write them in forms.py and html template
Here i am providing the code for froms.py where i am using BetterModelForm and it is for Python 2.7
from form_utils.forms import BetterModelForm, BetterForm
class CampaignMainForm(BetterModelForm):
class Meta:
model = Campaign
exclude = ['campaign_create_send_id',
'list_id',
'name',
'subject',
'created_by',
'send_datetime',
'send_timezone',
'from_name',
'from_email',
'reply_to_email',
'preview_url',
'template',
'text_file',
'assets',
'is_published',
'is_canceled',
'template_version',
'campaign_version']
fieldsets = [
['group',{
'fields': ['group'],
'legend': "Recipients",
}],
['template',{
'fields': ['stored_template'],
'legend': "Template",
}],
['templatedata',{
'fields': ['stored_template_data', 'stored_template_data_validated'],
'legend': "Template Content",
}],
]
I have updated my project from Python 2.7 and Django 1.8 to Python 3.7 and Django 3.0
Where BetterModelForm and fieldsets are suuported in Python 2.7
My first question is that can i use fieldsets in Python 3.7 ? If yes how can i write them in forms.py and html template
Here i am providing the code for froms.py where i am using BetterModelForm and it is for Python 2.7
from form_utils.forms import BetterModelForm, BetterForm
class CampaignMainForm(BetterModelForm):
class Meta:
model = Campaign
exclude = ['campaign_create_send_id',
'list_id',
'name',
'subject',
'created_by',
'send_datetime',
'send_timezone',
'from_name',
'from_email',
'reply_to_email',
'preview_url',
'template',
'text_file',
'assets',
'is_published',
'is_canceled',
'template_version',
'campaign_version']
fieldsets = [
['group',{
'fields': ['group'],
'legend': "Recipients",
}],
['template',{
'fields': ['stored_template'],
'legend': "Template",
}],
['templatedata',{
'fields': ['stored_template_data', 'stored_template_data_validated'],
'legend': "Template Content",
}],
]
here is my html template for python 2.7
{% for fieldset in form.fieldsets %}
<fieldset{% if fieldset.name != "templatedata" %} class="accordion-group"{% endif %}>
{% if fieldset.name != "templatedata" %}
<legend><bull class="bull {% if fieldset.legend == "Recipients" %}{% if object.group != None %}label-color-0d0{% endif %}{% elif fieldset.legend == "Template" %}{% if object.stored_template != None %}label-color-0d0{% endif %}{% else %}label-color-0d0{% endif %}"></bull> {{ fieldset.legend }}
<small class="muted">{% if fieldset.legend == "Template" %}{% if object.stored_template != None %}{{ object.stored_template.name }}{% endif %}{% endif %}{% if fieldset.legend == "Recipients" %}{% if object.group != None %}{{ object.group }} ({{ email_valid_count }} emails){% endif %}{% endif %}</small>
</legend>
.....
.....
{% endfor %}
Now please help me how can i write in python 3.7 forms.py and same functionality of html template in python 3.7

Related

Uncaught Twig_Error_Syntax: Unknown "json_encode" function opencart

I have an issue with a twig file in my opencart 3.0.2.0 webshop. The error that I got is:
Uncaught Twig_Error_Syntax: Unknown "json_encode" function in
"default/template/extension/module/notification.twig" at line 22. in
/home/mk4design/public_html/system/library/template/Twig/ExpressionParser.php:574
Stack trace:#0
The complete twig file is below and there are 3 lines with the json_encode that are not correct.
Can anyone help me to give me a lead to solve this problem?
<div id="fnotification-{{ module }}" class="f-notification animated">
<div class="notification-block">
{% if (notification['close_status']) %}<button class="fclose-notification">×</button>{% endif %}
<div class="notification-image">
{% if (custom_icons) %}
{% if (thumb) %}
<img src="{{ thumb }}" title="{{ title }}" alt="{{ title }}">
{% endif %}
{% else %}
<i class="fas fa {{ font_icon }}"></i>
{% endif %}
</div>
<div class="notification-text-block">
<div class="notification-title">{{ title }}</div>
<div class="notification-text"></div>
</div>
</div>
</div>
<script type="text/javascript">
$('#fnotification-{{ module }}').fNotification({
uId : {{ module }},
Cities : {{ json_encode(city) }},
Names : {{ json_encode(cname) }},
Products : {{ json_encode(products) }},
Amount : [{{ min_amount }}, {{ max_amount }}],
Text : '{{ text }}',
// Time Duration
DelayFirstMin : '{{ delay_first }}',
DisplayTime : '{{ display_time }}',
DelayMin : '{{ delay_between }}',
AnimationEffectOpen : '{{ in_animation }}',
AnimationEffectClose : '{{ out_animation }}',
DisplayTimes : '{{ cutomer_session > 0 ? cutomer_session : 0 }}',
CloseLifetime : '{{ cookie_time }}',
// Position
position: '{{ position }}',
postionTop : {{ top != '' ? top : '' }},
postionBottom : '{{ bottom != '' ? bottom : '' }}',
postionLeft : '{{ left != '' ? left : '' }}',
postionRight : '{{ right != '' ? right : '' }}',
NotificationSound :'',
// Style
borderRadius : '{{ border_radius }}',
borderWidth : '{{ border_width }}',
borderColor : '{{ border_color }}',
backGround : '{{ bg_color }}',
TextColor : '{{ text_color }}',
LinkColor : '{{ link_color }}',
Width : '{{ popup_width }}',
Height : '{{ popup_height }}',
// Close Button Color
BtnCloseColor : '{{ close_color }}',
BtnCloseHoverColor : '{{ close_hover_color }}',
});
</script>
Twig syntax for json is data|json_encode()
Just change:
Cities : {{ json_encode(city) }},
Names : {{ json_encode(cname) }},
Products : {{ json_encode(products) }},
To:
Cities : {{ city|json_encode() }},
Names : {{ cname|json_encode() }},
Products : {{ products|json_encode() }},

Spaceless tag inside script block

I'm trying to inline this so the rendered page isn't 100,000 lines long.
It seems the spaceless tag does nothing here.
Input:
{% for log in logs -%}
{% spaceless %}
{
id: {{ log.id }},
description: '{{ log.description }}'
},
{% endspaceless %}
{% endfor -%}
Expected Output:
{id:17186,description:'Test Log'},
{id:17187,description:'Test Log 2'},
Output:
{
id: 17186,
description: 'Test Log'
},
{
id: 17187,
description: 'Test Log 2'
},
Like the documentation says, you can "use the spaceless tag to remove whitespace between HTML tags, not whitespace within HTML tags or whitespace in plain text."
You can instead control whitespace e.g. like this:
{% for log in logs -%}
{
{{- 'id' }}: {{ log.id }},
{{- 'description' }}: '{{ log.description }}'
{{- '},' }}
{%- endfor %}
The above produces this:
{id: 17186,description: 'Test Log'},{id: 17187,description: 'Test Log 2'},
The downside is that the Twig code is much uglier.
Here's an alternative using string interpolation:
{% for log in logs -%}
{
{{- 'id' }}: {{ log.id }},
{{- 'description' }}: {{ "'#{log.description}'" -}}
},
{%- endfor %}
{id: 17186,description: 'Test Log'},{id: 17187,description: 'Test Log 2'},
The Twig code looks now better, but notice that single quotes got converted to '. You may or may not want to do something about that.
You could also do like the documentation suggests and use a third-party library like Tidy.

Grav - Using shortcodes in twig

Following this question, I have found that there is a plugin named shortcodes which might be helpful here.
I have installed the plugin and created a new page:
---
title: slide1
media_order: slider1.jpeg
taxonomy:
category:
- 'Home Slider'
process:
markdown: true
twig: true
routable: false
cache_enable: false
visible: false
---
[section name="slide_title"]
Precisión y Calidad
[/section]
[section name="slide_subtitle"]
La máxima calidad y seguridad para nuestros clientes
[/section]
[section name="slide_link"]
www.google.es
[/section]
Then I have an invoked partial which makes:
{% for slide in taxonomy.findTaxonomy({'category': 'Home Slider'}) %}
<span>TEST</span>
<span>{{ slide.title }}</span>
<span>{{ slide.shortcode.section.slide_title }}</span>
<span>{{ slide.shortcode.section.slide_subtitle }}</span>
<span>{{ slide.shortcode.section.slide_link }}</span>
{% endfor %}
But this generates just:
TEST
slide1
And it does not output the shortcode section contents.
The documentation you linked to shows the correct syntax when you are not dealing directly with the page variable:
{{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_title }}
{{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_subtitle }}
{{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_link }}

Showing category thumbnails in opencart 3 menu

I'm looking for a way to show the category thumbnail in the menu. Is this possible?
Specifically for OpenCart 3.
OpenCart 3.0.2.0, Default theme
Open this file:
catalog\controller\common\menu.php
Find:
$this->load->model('catalog/product');
Add after it:
$this->load->model('tool/image');
Find:
$data['categories'][] = array(
Replace with:
if($category['image']){
$image = $this->model_tool_image->resize($category['image'], 30, 30);
} else {
$image = false;
}
$data['categories'][] = array(
'image' => $image,
Then open this file:
catalog\view\theme\default\template\common\menu.twig
Find:
{{ category.name }}
There are three occurrence, add before first and last one:
<img src="{{ category.image }}" alt="{{ category.name }}"/>{% endif %}
Here is the result:
for submenu use:
$children_data[] = array(
'image' => $child['image'] ? $this->model_tool_image->resize($child['image'], 20, 20) : false,
or
'image' => $child['image'] ? $this->model_tool_image->resize($child['image'], 20, 20) : $this->model_tool_image->resize('your-default-image.jpg', 20, 20),
in template
{% for child in children %}
<li>{% if child.image %}<img src="{{ child.image }}" alt="{{ child.name }}" />{% endif %}{{ child.name }}</li>
{% endfor %}
If missed something, follow here

How to add a style to a {{ form_label(form.name) }}

I'm using Symfony 3.0, in this moment I have this code:
{{ form_label(form.name, 'Name of the card', { 'attr': {'style': 'color:white'}}) }}
Thank you :) !
You need to use label_attr for labels, attr is for the widget function.
{{ form_label(form.name, 'Name of the card', { 'label_attr': {'style': 'color:white'}}) }}
According to the documentation rendering form row involves three functions
{{ form_label(form.name) }}
{{ form_errors(form.name) }}
{{ form_widget(form.name) }}
You can replace form_label(form.name) with your layout
<div class="form_label">Name of the card</div>
<div class ="form_errors">{{ form_errors(form.name) }}</div>
{{ form_widget(form.name) }}

Resources