Why is variable name important in multiwidget template? - django

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?

Related

Djang multiple levels of template extension while keeping the elements of the upper levels

I have 3 templates as following:
base.html (the top level template, as usual).
<body>
<div id="header">
...
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
category.html :
{% extends "base.html" %}
{% block content %}
<div class="category_row">
<div id="menu_category_display">
{% for category in menu_categories %}
{% with category_button="menu_"|add:category.name|lower %}
<button class="button category_buttons {{category_button}}"
onclick="showItem('{{category_button}}','category_buttons')">
{{category}}
</button>
{% endwith %}
{% endfor %}
</div>
</div>
<div class="item_row">
{% block level_2_content %}
{% endblock %}
</div>
{% endblock %}
and item.htlm
{% extends "menu/category.html" %}
{% block level_2_content %}
<div id="test_div">
{% for item in menu_items %}
<p>{{item.name}}</p>
{% endfor %}
</div>
{% endblock %}
When item.html is rendered, all the elements that were rendered by category.html are gone. Only the elements of the base.html are retained.
How do I retain all elements in all parent templates instead of just base.html? A general method to do this for more than 3 levels of extension like in my case?
Update with more detailed code for category.html. The entire category_row div does not appear in item.html. How do I retain them?
views.py
def menu_category_view(request, table_pk):
menu_categories = Category.objects.all()
menu_items = Item.objects.filter(available=True)
return render(request,
'menu/category.html',
{'menu_categories': menu_categories,
'menu_items': menu_items,
'table_pk': table_pk})
def menu_item_view(request, menu_category, table_pk):
category = Category.objects.get(name=menu_category)
menu_items = Item.objects.filter(available=True,
category=category.pk)
return render(request,
'menu/item.html',
{'menu_items': menu_items})
Folder structure: both templates sit in menu/templates/menu of app menu. However, base.html sits in root/templates (not in app menu.
Update: I have tried modifying both templates in various ways to debug. What I find is that the static content gets extended, but the dynamic content does not. So my question becomes: does Django template allow dynamic content to be extended to child / grandchild templates?
You probably don't want to use "extends" here but instead "include" to render 'item.html' within the parent document.

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 %}

Translate django variable with html template value

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.

Adding div directly into ModelForm in django

Is there way to add div tag directly into ModelForm in django?
I want to get something like this:
<input></input>
<input></input>
.
.
<div class="someClass">
<input></input>
<div>
<input></input>
Or i need to do this in html page?
UPDATE
This is generated by:
{% for field in formFields %}
{{field}}
{% endfor %}
I want for some inputs to be div around it.
Add div in your for loop like this:
{% for field in formFields %}
{% if field.name == 'xxx' %}
<div class="someClass">{{ field }}</div>
{% else %}
{{ field }}
{% endif %}
{% endfor %}

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