How would you stop Deform from escaping HTML in field titles or descriptions when rendering? My current best solution is to search/replace the returned rendered HTML string with what I need.
Deform by default will escape all HTML characters to HTML entities, I want to add an tag in one of the field descriptions.
Copy the default widget template and modify it to allow unescaped entries.
Descriptions are placed by mapping.pt. It cannot be overridden per widget basis - the mapping template is the same for all the items in the form. You can override mapping by passing item_template to your widget container (Form, Form section). Non-tested example:
# No .pt extension for the template!
schema = CSRFSchema(widget=deform.widget.FormWidget(item_template="raw_description_mapping"))
You can use TAL structure expression to unescape HTML.
E.g. example raw_description_mapping.pt for Deform 2:
<tal:def tal:define="title title|field.title;
description description|field.description;
errormsg errormsg|field.errormsg;
item_template item_template|field.widget.item_template"
i18n:domain="deform">
<div class="panel panel-default" title="${description}">
<div class="panel-heading">${title}</div>
<div class="panel-body">
<div tal:condition="errormsg"
class="clearfix alert alert-message error">
<p i18n:translate="">
There was a problem with this section
</p>
<p>${errormsg}</p>
</div>
<div tal:condition="description">
${structure: description}
</div>
${field.start_mapping()}
<div tal:repeat="child field.children"
tal:replace="structure child.render_template(item_template)" >
</div>
${field.end_mapping()}
</div>
</div>
</tal:def>
You also need to modify your Pyramid application to load overridden Deform templates when constructing WSGI application with Pyramid's Configurator:
from pyramid_deform import configure_zpt_renderer
configure_zpt_renderer(["mypackage:templates/deform", "mypackage2.submodule:form/templates/deform"])
Related
I'm facing a problem when I embed HTML code in my python's view.py
Basically, my objective is to customize the color of certain words only (based on the input). I want to do it by modifying the view.py
For example (my view.py):
def home(request):
form = request.POST.get('uncorrected')
texts = str(form) + '<span style="color: red">test text but this section is red</span>'
return render(request, 'corrector/home.html', {'text': texts})
Inside my index.html:
<textarea type="text" id="textarea" name="uncorrected">
{{ text }}
</textarea>
However, when I type "my text" in the textarea it displays only:
my text <span style="color: red">test text but this section is red</span>
It doesn't make the text red, it directly displays the code.
How can I make it work?
Django automatically escapes HTML to prevent XSS attacks. In order to render HTML as HTML, you just pipe in safe.
{{ text|safe }}
Django will still escape the HTML, but it will render it as well
Hi I'm new to using Wagtail and I'm working on a client website. What I aim to do is to dynamically link my wagtail pages to our sidebar, which is currently in our base.html in the main app folder's templates directory, the one with settings.py.
I was wondering if there's a way to render a call to action for the base.html here. Or if I should make a separate app instead and create a base.html there, which extends to all the other templates I'll use for the rest of the website.
Thank you!
edit:
Above is the current home page I'm working with. The sidebar right now is just hard-coded since I haven't worked on that, and I want to know what the rest of the page looks like while I work on the main content.
the sidebar above is coded as so:
<!-- in biodept/templates/base.html -->
{% wagtailuserbar %}
<div class="container main-container">
<div class="row">
<!-- Nav bar not mobile -->
<nav class="nav" id="nav-1">
<a class="nav-link nav-desktop-link nav-desktop-link-active" href="#">HOME</a>
<a class="nav-link nav-desktop-link" href="#">BIOMEDICINE</a>
<a class="nav-link nav-desktop-link" href="#">ECOLOGY & SYSTEMATICS</a>
<a class="nav-link nav-desktop-link" href="#">MOLECULAR BIO & BIOTECH</a>
<a class="nav-link nav-desktop-link" href="#">PROJECTS</a>
<button class="dropdown-btn nav-link nav-desktop-link">PROGRAMS<ion-icon style="float: right; padding-top: 0.25vw;" name="caret-down-outline"></ion-icon></button>
<div id="btn-t" class="dropdown-container">
<a class="nav-link dropdown-nav-desktop-link" href="#">UNDERGRADUATE</a>
<a class="nav-link dropdown-nav-desktop-link" href="#">GRADUATE</a>
</div>
<a class="nav-link nav-desktop-link" href="faculty.html">FACULTY PAGES</a>
<a class="nav-link nav-desktop-link" href="#">BIODIVERSITY LABORATORY</a>
</nav>
{% block content %}{% endblock %}
</div>
</div>
Again the base.html is in the same directory as where the settings.py is. BioDept is the project's name.
Note: Based on the updated question, it looks like this is unrelated to StreamField but it is a question about how to implement a menu based on the Wagtail page structure.
Wagtail does not come with a built in way to render menus, this is because it is going to be something specific to every Wagtail site and any generic solution will likely only cover a small set of cases. However, when getting started this can be a bit confusing.
Wagtail, does come with a way to indicate that a page should be shown in menus though, this is part of every Page model.
You can see this in the model's reference here
https://docs.wagtail.io/en/latest/reference/pages/model_reference.html#wagtail.core.models.Page.show_in_menus
User's can edit this value on the 'promote' tab, plus the docs above let you define what the default value should be (however, existing pages will need to be updated another way).
Implementing a Menu
Here are three ways to implement a menu and use this as a template tag or template include in your project.
View the Bakerydemo code
The bakerydemo is a nice basic reference for a Wagtail implementation, this may not explain why but might be enough for you to get started.
template tag definition - https://github.com/wagtail/bakerydemo/blob/master/bakerydemo/base/templatetags/navigation_tags.py
header include template - https://github.com/wagtail/bakerydemo/blob/master/bakerydemo/templates/includes/header.html
base.html (layout) template - https://github.com/wagtail/bakerydemo/blob/master/bakerydemo/templates/base.html
Follow a tutorial
Googling 'Wagtail navigation' or 'Wagtail menus' can help, but this link below appears to be up to date and walks you through really nicely on how to build a basic Wagtail menu and then enhance it to using an extension (added below)
https://www.accordbox.com/blog/wagtail-tutorial-12-how-create-and-manage-menus-wagtail-application/
Install an extension package
This package appears to give a robust solution, but avoid using it if you can get what you need without adding another dependency (my opinion)
https://wagtailmenus.readthedocs.io/en/stable/overview.html
How to keep the original text formatting (e.g. new lines, bold text) in TextField django admin.
For example, I want to place text on two lines. But in html code I see only one line "Line1 Line2"
I found solution --- usage of html tags, but it's not suitable for me.
How to keep the original text formatting without html tags in django?
Any design advises?
UPD 1
I added "< pre >" tag, and how can I remove scroll at the end?
mycode.html
{% block content %}
<div class="container" style="margin-top: 20px">
<div class="row">
<div class="col-md-8">
<h1>{{vacancy}}</h1>
<h2>{{vacancy.salary}}</h2>
<h3>{{vacancy.company_key.name}}</h3>
<p>{{vacancy.city}}</p>
<p><pre>{{vacancy.text| safe}}</pre></p>
</div>
</div>
</div>
{% endblock %}
Lets say I have following HTML Code
<div class="12">
<div class="something"></div>
</div>
<div class="12">
<div class="34">
<span>TODAY</span>
</div>
</div>
<div class="12">
<div class="something"></div>
</div>
<div class="12">
<div class="something"></div>
</div>
Now If I use driver.find_elements_by_class_name("something") then I get all the classes present in the HTML code. But I want to get classes only after a specific word ("Today") in HTML. How to exclude classes that appear before the specific word. Next divs and classes could be at any level.
You can use search by XPath as below:
driver.find_elements_by_xpath('//*/text()[.="some specific word"]/following-sibling::div[#class="something"]')
Note that you might need some modifications in case your real HTML differs from provided simplified HTML
Update
replace following-sibling with following if required div nodes are not siblings:
driver.find_elements_by_xpath('//*/text()[.="some specific word"]/following::div[#class="something"]')
I am using simple form in web2py. In controller I create a form like this:
form=FORM(DIV(INPUT(_id='itemId',_name='itemId',_value='102737069',requires=[IS_NOT_EMPTY("please input correct itemId"),IS_LENGTH(maxsize=12)]), INPUT(_type='submit',_value='search_key',_name='search'), INPUT(_type='submit',_value="random_key",_name='random')))
In corresponding html the form is embedded in a div tag and I want the form align to center. How to set it? Thanks.
EDIT:
This is the html code. Now I can only use two to align the form to center approximately.
I am new in css.
<div class="top">
<div class="header_left">
<div class="msg">
please submit the itemId...
</div>
</div>
<div class="header_right">
<br/><br/>
{{=form}}
</div>
</div>
<br/>
<br/>
<br/>
In the beginning div tag that the form is in, add align="center".
So if I'm understanding your issue correctly then you'll want to put align="center" in the <div class="header_right"> like found in this jsfiddle.