I'm using jinja to render a flask-wtf submit button as follows:
{{ wtf.form_field(form.submit) }}
This results in a button formatted in btn-default bootstrap format (white). I'd like to change this to btn-success bootstrap format (green).
How can I achieve this?
As suggested by #dpgaspar the solution was to use button_map as follows:
{{ wtf.form_field(form.submit, button_map={'submit':'success'}) }}
If you are using wtf.quick_form, use the form like this.
{{ wtf.quick_form(form, button_map={'submit':'success'}) }}
I presume you are also using flask-bootstrap.
On the flask-bootstrap Jinja2 macros you have:
{% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
{{field(class='btn btn-%s' % button_map.get(field.name, 'default'), **kwargs)}}
{% endcall %}
You should use if you can the button_map to do it [see details in comments below]
If you are using flask-bootstrap, use the form with button_map as suggested by #dpgaspar,
For whole form - wtf.quick_form:
{{wtf.quick_form(delete_form, button_map={'name_of_field': 'danger'})}}
For individual field, wtf.form_field:
{{wtf.form_field(delete_form.delete, button_map={'delete': 'success'})}}
Flask-Bootstrap official documentation says:
button_map – A dictionary, mapping button field names to names such as primary, danger or success. Buttons not found in the button_map will use the default type of button.
Related
I have forms.py file with usercreation form inside of it. It works just fine, but the only way I can display fields in it(first_name, last_name, username, email, password and password confirmation is by writing {{ form.as_p }}. I want to display it the way I could style it. Are there any ways to do that?
You can add the css style by using form widgets
class BasicForm(forms.Form):
first_name = models.CharField(...,
widget=forms.TextInput(attrs={'class' : 'myclass'})
last_name = models.CharField(...,
widget=forms.TextInput(attrs={'class' : 'myfieldclass'})
later use class css design
You can write your own HTML at various degrees of granularity. It's described in the documentation.
Crispy Forms is a widely used package that makes fancy form layout a lot easier. Also you can do most of the work in Python by coding a Crispy Layout object, rather than by writing a complicated HTML template.
I'm learning Drupal8 and Twig with Chaz Chumley's book 'Drupal 8 Theming with Twig'.
When I put in the code provided I don't get the desired result. (Chapter 3, Filters)
The book says to add the following to the page.html.twig file:
{% filter upper %}
<p>{{ name }} is the best cms around.</p>
{% endfilter %}
but the page outputs
<P>DRUPAL IS THE BEST CMS AROUND.</P>
(Showing the html tags on the page as shown here)
Is there something I'm missing to have the twig filter not change the HTML tags? or is the only solution to put the filter inside the tag? but this filter is supposed to "wrap sections of HTML and variables" so why is it affecting HTML tags?
You can put the filter around just the text, so it ends up as:
<p>{% filter upper %}{{ name }} is the best cms around.{% endfilter %}</p>
You can test your twig code here: https://twig.stapps.io/
Can you give this a try:
<p>{{ 'your text'|upper }}</p>
Also, check out https://drupal.stackexchange.com/ if you have any more drupal related questions.
The Django UserCreationForm is very useful and easy to use functionally, however, it just throws everything onto the page so all the TextFields are in one line.
Is there anyway to layout and style the form? Specifically to put labels and input fields on a new line and possibly make it possible to apply some CSS to the whole form and individual parts of the form.
Use BoundField and custom filters, and correct names of attributes of UserCreationForm class
Uses the BoundField class to display HTML or access attributes for a single field of a Form instance. And add CSS with a filter in the templatetags folders with an __init__.py, if have error remember add templatetags in your INSTALLED_APP like to 'app.name.templatetags' like:
{{ form.username.label_tag }}
{{ form.username|addclass:'form-control' }}
{{ form.password1.label_tag }}
{{ form.password1|addclass:'form-control' }}
{{ form.password2.label_tag }}
{{ form.password2|addclass:'form-control' }}
View source
https://github.com/django/django/blob/master/django/contrib/auth/forms.py
The passwords are named 'password1' and 'password2'
Every django form has three methods:
as_p()
as_ul()
as_table()
See the outputting forms as html section in the docs. You can style this output as you want.
If you want even more control over form's html then read this section of the same documentation.
In your forms.py,you can inherit UserCreationForm and set css style.(How to set css for forms CSS styling in Django forms)
I need to provide page content reference list (it should contain references on sections on page).
The only way which I can see is to use page.content and parse it, but I stumbled on problem with data evaluation. For example I can pull out this string from page.content: {{site.data.sdk.language}} SDK but there is no way to make jekyll process it, it outputs as is.
Also I want to make it possible to create cross-pages links (on specific section on page, but that link generated by another inclusion and doesn't persist in page.content in HTML form).
Is there any way to make it evaluate values from page.content?
P.S. I'm including piece of code which should build page content and return HTML with list (so there is no recursions).
P.P.S. I can't use submodules, because I need to run this pages on github pages.
Thanks.
Shouldn't {{ site.data.sdk.language | strip_html }} do it? I don't know, most probably I didn't understand the problem. Can you elaborate more? Maybe provide a link to a post you're referring to?
Thinking about the similar
{% assign title = site.data.sdk.language %}
which is a stock Liquid tag and does the job well, so instead of
{% section title={{site.data.sdk.language}} %}
write your code as
{% section title = site.data.sdk.language %}
The key here is that once you enter {%, you're in Liquid. Don't expect Liquid to go "Inception" all over itself. {{ is just a shorthand for "print this to output", but an parameter passing is not output, it's just reading a variable.
You should be able to also go wild and do:
{% section title = site.data.sdk.language | capitalize %}
For more re-read the docs: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
This feels like a really silly issue, but I'm confused by the behavior of django-zinnia, a blog creation module.
When I test enter a plain text post, it appends each sentence with html < p > tags the browser doesn't read as html.
Example, if I enter this into the database (no html):
The entry from the db renders on page itself like this as if the < p > markup was plain text:
Within Zinnia, these html tags are being generated as part of the {{ object_content }} object in _entry_detail_base.html
<div class="entry-content">
{{ object_content }}
</div>
I've looked through the entry.py models within Zinnia and I'm having trouble identifying where these tags are coming from or how they're being passed in in a way the browser doesn't interpret them for what they are (html). Is there a filter I can apply that might solve this? thanks
That's the default behavior for Django templates. Use {{ object_content|safe }} or {% autoescape off %} {{ object_content }} {% endautoescape %} (for multiple variables) to prevent html entities from being escaped.
Note that using the safe filter doesn't automatically mean the output is not escaped if you use another filter after it.
Check the Zinnia's source code: https://github.com/Fantomas42/django-blog-zinnia/blob/master/zinnia/templates/zinnia/_entry_detail_base.html
It's using |safe template tag:
<div class="entry-content">
{{ object_content|safe }}
</div>