django form field label as html - django

I am loading a form using following template tag -
{{ form.as_table }}
Form got radio button, and the select items for it got using following form field -
forms.RadioSelect(choices=[ (o.choice_value, o.choice_name) for o in Choice.objects.filter()])
one of the choice_name got html elements into it, which gets loaded with those tags instead of actual HTML rendering.
If it would be regular template variable, I can do {{ some_variable|safe }} to load it as HTML, any idea how to do it in case of form tag.

You can use template tags and filters anywhere in your Python code, just import them and use them:
from django.template.defaultfilters import safe
forms.RadioSelect(choices=[(o.choice_value, safe(o.choice_name))
for o in Choice.objects.filter()])

Related

How do I remove <p> tag from django richtextfield?

I am getting p tag displayed after I post an image or question with ckeditor editor in Django. .How can I remove the tag and get the image or text be displayed correctly.
You need to explicitly state that the content in template should be considered safe. You can use safe filter (Django docs) to do that so for variable text_content in your template you should do {{ text_content | safe }}.

setting a dynamic data attribute on flask wtforms jinja select

Have a flask wtforms select field and trying to incorporate htmx ajax call, which has dashes in data attributes so I found a solution on SO like the following:
{{ form.dia(class="form-select", **{'hx-post': "/pulleys/{{pulley_id}}/hub_type", 'hx-target': "#hub-shaft-selection", 'hx-swap': "innerHTML"}) }}
but the {{pulley_id}} doesn't get parsed by the template, I guess it's already inside another {{ }}.
Is there a way to build the dynamic part as shown above so it ends up as
'hx-post'="/pulleys/1/hub_type"
when fully rendered for pulley_id=1
If pulley_id is a variable either inside a loop or passed into render_template your should be able to format the string:
{{ form.dia(class_="form-select", **{'hx-post': "/pulleys/%s/hub_type"|format(pulley_id), 'hx-target': "#hub-shaft-selection", 'hx-swap': "innerHTML"}) }}
Note: you also want class_ not class if you're trying to set the HTML class attribute.

How to render html text from database inside a django template using template tags

I save a text into my database using django form, textarea widget and bootstrap wysiwyg, but when I try to render it into my template using just the variable name
{{ text }},
I get rendered just the text with html tags like this:
what I want to do is show the formatted text in html.
If you have the formatted HTML in your DB-field, and you absolutely sure is it safe, then try safe filter
{{ tablename.fieldname|safe }}
or
{{var|safe}}
See the doc.

djangocms-snippet which contains {% %} doesnot show up as placeholder content

I am trying to make my footer a editable from frontend.. using placeholder and inserting footer snippet.
yet, my footer snippet contains django template language
e.g.
Terms and Conditions
as a result, the placeholder content is not showing up, if i remove the django specific things
Terms and Conditions
it is working.
how can I make it work with django reverse url?
I could have given hard coded path but i want the path translatable so i need to reverse by url's name.
If I've got you right, just create custom tag and store templates in database:
from django.template import RequestContext, Template
#register.simple_tag
def footer(request):
snippet = Snippet.object.get(name='footer')
template = Template(snippet.html)
return template.render(RequestContext(request))
{% footer request %}
use this syntax:
{% url 'terms_conditions' as the_url %}
Terms and Conditions
use smartsnippets
These has the capability of rendering django tags

Django textarea widget <p>

In django admin I am using the textarea widget.
When I save data, it is saved inside a <p></p> tag.
I dont want this - any solutions, I just want to get the data out without being wrapped in a <p>.
Any suggestions?
I don't know how to save it without html coding, but maybe it is enough to you to display without that . Just put a |safe in the template after the variable name, example:
{{ variable|safe }}
http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs