Translate django variable with html template value - django

I am trying to translate like this:
<div class="col-sm-7 section">
{{ template |safe }}
</div>
template = <div class="row">
<div class="calc-head">{% trans "Calculations" %}</div>
</div>
But the {% trans "Calculations" %} is not working for me. Can anyone help me

Why are you using template as a variable? Did you add it to the page as a string context object?
Save your template as an html file, remember to
{% load i18n %}
on top of the file and then put your code (with html content) inside:
{% blocktrans %}
{# Your html markup here #}
{% endblocktrans %}
And then add it to the page where you need it as:
{% include 'folder/name.html' %}
I hope that helps.

Related

Wagtail page context in Streamfield template

I have StructBlock with it's own template, within a StreamField. I'm trying to access the page object from said template.
{% load wagtailcore_tags article_tags %}
{% article_constants as constants %}
<div id="interactions__combo__addition" class="col-md-6">
<h3>
{% include_block page.translated_title %}
+
<span id="interactions-combo-addition-temp">?</span> =
</h3>
<div alt="dangerous to synergy bar" style="height:10px; width:100%">
</div>
</div>
<div class="interactions__combo__result col-md-5">
<h3 class="interactions__combo__result__title">
{{ constants.select_element }}
</h3>
<p class="interactions__combo__result__description">
{{ constants.none_selected_text }}
{% include_block page.colour %}.
</p>
</div>
{% include_block page.colour %} and {% include_block page.translated_title %} render nothing.
Thank you in advance for your help.
You should use the {% include_block %} tag when outputting the StreamField on your page template. For example, if your StreamField is called body, use {% include_block page.body %} on your page template. This will ensure that the context variables from the outer template (including page) are available in your StructBlock's template - if you use {{ page.body }} instead, the StructBlock template will render, but won't have access to the variables from the outer template.
Don't use {% include_block %} for fields of page that are not StreamFields, such as page.translated_title.

Why is variable name important in multiwidget template?

In my form i have a Multiwidget with 3 Textinputs.
I want to customize html output so in 1.11 i have to use new template api.
Clear so far.
My custom multiwidget template(99% copy/paste from docs) looks like this:
<div class="row">
{% for widget in widget.subwidgets %}
<label class="col-md-2">Label</label>
<div class="col-md-2">
{% include widget.template_name %}
</div>
{% endfor %}
</div>
But i don't like using the same variable name 'widget' for multiwidget and subwidget.
So i replace
{% for widget in widget.subwidgets %}
{% include widget.template_name %}
with
{% for subwidget in widget.subwidgets %}
{% include subwidget.template_name %}
But it doesn't work. I get the same text input 3 times and this input has id, name and value from multiwidget, not from subwidgets(id without "_0", compressed value)
Is it a bug or my misunderstanding?

Django template include overwrite <h1> tag

I have the following html files.
banner.html
<header class="intro2">
<div class="intro-body">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>{% block banner %}Bannertest{% endblock banner %}</h1>
</div>
</div>
</div>
</div>
</header>
test.html
{% extends 'banner.html' %}
{% block banner %}
Test
{% endblock banner %}
I'm new to Django but I would expect the H1 title to be updated to say Test instead of Bannertest?
What am I doing wrong?
You need to extend your main html not to include
replace
{% include 'banner.html' %}
to
{% extends "banner.html" %}
more details here: include-vs-extends
your full new html:
{% extends 'banner.html' %}
{% block banner %}Test{% endblock banner %}
For block overriding you must use {% extends 'banner.html' %} instead of {% include %}
So instead of making small fragments for include, like in php, standard approach is to make a full template, say base.html, when extend it in child templates.
{% include %} tag is better suits for widgets with parameters, e.g. {% include '_form.html' style='light' some_param=some_value %}

How to make a reusable template in Django?

What is the Django way of creating a reusable template?
Example: Suppose a lot of my pages contain a "Latest News" box and following the DRY principle, I would like to define it once and reuse it in the other pages. How would I do this with Django (or Jinja2) templates?
Reading through Django's Template Documentation I get the impression that Django templates offer "top-down" inheritance where the sub-template itself determines in which super-template it is going to be embedded:
<!-- Super-template (not valid, for illustration): -->
<html>
<head><title>Title</title></head>
<body>{% block content %}{% endblock %}</body>
</html>
<!-- Sub-template: -->
{% extends "base.html" %}
{% block content %}
<div class="latest-news">News</div>
{% endblock %}
So what is the technique to reuse a block (a sub-template) in several places?
The most flexible way to reuse template fragments is to define an inclusion_tag. You can pass arguments to your custom tag, process them a bit in Python, then bounce back to a template. Direct inclusion only works for fragments that don't depend on the surrounding context.
Quick example from the docs:
In app/templatetags/poll_extras.py register the tag with a decoration:
from django import template
register = template.Library()
#register.inclusion_tag('results.html')
def show_results(poll):
choices = poll.choice_set.all()
return {'choices': choices}
In app/templates/results.html:
<ul>
{% for choice in choices %}
<li> {{ choice }} </li>
{% endfor %}
</ul>
Calling the tag:
{% load poll_extras %}
{% show_results poll %}
What you're looking for, is {% include "template.html"%} from Django docs.
If you need to use {% block %} you can only do that via the {% extend %} approach. Otherwise, you can use {% include 'some.html' %} to include a bit of HTML in multiple places.
The unofficial Django Reusable App Conventions recommends using these block names:
{% block title %}
{% block extra_head %}
{% block body %}
{% block menu %}
{% block content %}
{% block content_title %}
{% block header %} {% block footer %}
{% block body_id %} {% block body_class %}
{% block [section]_menu %} {% block page_menu %}
If everyone stuck to these conventions, it should make this problem easier. Follow the link to see the description of each block.
Example of using {% include %} tag
All data comes from Django back-end
Many values are passed to card_template.html using include tag in page1.html
card_template.html
<style>
.choices_div {
border-radius: 5rem;
}
.card-footer {
background-color: transparent;
border: transparent;
}
</style>
<div class="col mb-5 px-4">
<div class="card h-100 w-100 jumbotron choices_div {{ bg_color|default:'' }}">
<div class="card-body p-0">
<h3 class="card-title text-center">{{ card_title|capfirst }}</h3>
<ul class="card-text mt-3">
{% for c in card_body_list %}
<li>{{ c }}</li>
{% endfor %}
</ul>
</div>
<div class="card-footer text-center pt-4">
{% if get_post_request == 1 %}
<a class="btn btn-light" href="{{ href }}">{{ button_text }}</a>
{% else %}
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-light w-75" name="category"
value="{{ button_value }}">{{ button_text }}</button>
</form>
{% endif %}
</div>
</div>
</div>
page1.html
{% extends 'core/core.html' %}
{% block body %}
<div class="jumbotron bg-white">
<div class="container">
<div class="mb-5 text-center">
<h1>Choose user category</h1>
<h5>Once choosen, the user category cannot be changed</h5>
</div>
<div class="row row-cols-lg-2 justify-content-around">
{% for object in object_list %}
{% cycle 'bg_peacock' 'bg_sunset' 'bg_skin' 'bg_brown' as bg_color silent %}
{% include 'core/card_template.html' with card_title=object.category card_body_list=object.description get_post_request=2 button_text='Select' bg_color=bg_color button_value=object.id %}
{% endfor %}
</div>
</div>
</div>
{% endblock %}
As other answers have mentioned, the simplest approach is direct inclusion:
{% include 'mytemplate.html' %}
It is possible to modify the context of the rendered template (Or in simpler terms, to pass variables to the template) using
{% include 'mytemplate.html' with poll=poll %}
To use the traditional polls example, the template I would write would be:
<div class="stylish-poll">
{% for choice in poll.choices %} <!-- poll is a template variable -->
{% include 'choice_template.html' with choice=choice %}
{% endfor %}
</div>
Another potentially useful thing to know is that the only keyword prevents the template variable poll being passed into 'choice_template.html' which it would be by default. If you do not want the choice template to have access to {{ poll }} then the include statement looks like:
{% include 'choice_template.html' with choice=choice only %}
Documentation: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include
Aïe, my fault – the answer is given in the Django Reference (and not discussed in the aforementioned Django Template Documentation)…
So: Just use {% include sub_template_name %}.
even though the question is asked years ago, any way I will show you the method that worked for me.
base.html
In your base template you need to define all of your blocks that you need to reuse in your other templates,
<html>
<head>
<meta name="description" content="{%block description%}{%endblock%}">
<meta name="keywords" content="{%block keywords%}{%endblock%}">
<title>{%block title%}{%endblock%}</title>
</head>
<body>
<!---other body stuff--->
{%block content%}
{%endblock%}
</body>
</html>
home.html
{%extends 'base.html'%}
<!--you can reuse all blocks here-->
{%block description%}Django reusable blocks, for every bage{%endblock%}
{%block keywords%}django,block, resuable,meta,title,{%endblock%}
{%block title%}django reuseable blocks for title, meta description and meta keywords{%endblock%}
{%block content%}
<div>
<h1> reuse blocks</h1>
</div>
{%endblock%}

Do I need to create separate forms for simple and Ajax thing in Django

I have many forms which are working fine if i load them via normal http link.
The template is below
{% extends "app/base.html" %}
{% block title %}Create Account{% endblock %}
{% block media %} {% include "app/media_template.html" %} {% endblock %}
{% block heading %}Form{% endblock %}
{% block content %}
<div id="stylized" class="myform">
<form action="" method="post" enctype="multipart/form-data" >
<h1>Account form</h1>
<p>This is the basic look of my form without table</p>
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
<button type="submit">Sign-up</button>
<div class="spacer"></div>
</form>
</div>
{% endblock %}
But if i have to display via ajax then i just need the div box containing form only , i don't nedd all other html
So i want that if JS is not working then those forms still work via hyperlink.
I have 6 forms , do i have to create seoarate templae if i call via ajax
If you need to use part of your template in several places, you can put that part in a separate template, and include it wherever you need:
{% include "foo/bar.html" %}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#include