I would create a condition {% if ... %} that allows me to compare the {{ looged_user }} with the user's response {{ reply.user }}
The looged_user is the person connect
Reply.user is the response of a person
I would show only person logged answers
So I have try:
{% if '{{ logged_user }}' == '{{ reply.user }}' %}
or
{% if {{ logged_user }} == {{ reply.user }} %}
or
{% if logged_user == reply.user %}
But nothing works!
Nevertheless the logged user is equivalent to reply.user ...
My template :
{% for question in questions %}
{% for reply in question.reply_set.all %}
<hr>
{{ logged_user }}<br>{{ reply.user }} --> {{ question }} : {{ reply.answer }}
{% endfor %}
{% endfor %}
you can see that they are well equal ..
The result of the screenshot:
when I try to add the condition this shows me nothing.
Related
How could I get a next object in django for loop?
I mean is it possible to do like this {{ route.segments.all.ind }}?
{% for segment in route.segments.all %}
{% if segment.departure == departure %}
{% with forloop.counter as ind %}
<span style="margin-top: -5px; font-size: smaller">
$ {{ route.segments.all.ind }}
{{ segment.distance }}km
{{ segment.duration }}hour</span>
{%endwith %}
{% endif %}
{% endfor %}
I need to simplify this Django template,
{{ var.1 }}
{{ var.2 }}
{{ var.3 }}
{{ var.4 }}
{{ var.5 }}
var is Python list passed as context to the template
how do you convert the above template using a for tag construct. I tried this but does not work.
{% for i in var|length %}
{{ var.i }}
{% endfor %}
You can just do
{% for x in var %}
{{x}}
{% endfor %}
I am creating form using Django Form wizard and formsets.
def get_form(self, step=None, data=None, files=None):
initial_data_set = []
for x in some_list:
initial_data_set.append({
'title' : x.title,
})
data = {
'form-TOTAL_FORMS': '5',
'form-INITIAL_FORMS': '5',
'form-MAX_NUM_FORMS': '',
}
formset_class = formset_factory(TitleForm, extra =0)
formset = formset_class(data=data, initial=initial_data_set)
return formset
Template
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block content %}
{% if wizard.form.forms %}
{% for form in wizard.form.forms %}
{{ form.media }}
{% endfor %}
{% else %}
{{ wizard.form.media }}
{% endif %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{% if ingestable_upload %}
<tr>{{ form.as_inline_table }}</tr>
{% else %}
{{ form.as_table }}
{% endif %}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "Submit" %}"/>
</form>
{% endblock %}
I was able to see initial data in my form when I was not passing 'data'. However, not passing data was giving me formset.is_valid as False and there was None in the cleaned_data. So I created data{} and passed it as per the documentation here -
https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#understanding-the-managementform
Since I started passing data, form is not getting populated with initial data.
I have put debug statements in formsets.py under BaseFormSet() class. It is getting both data and initial data that I am passing.
I have been struggling with this for few days. Any help on how I can populate my form and get cleaned data will be great.
What you're doing is overriding your form initial data with your data object, data attribute is not necessary that data is passed on the {{ formset.management_form }} in your template.
There's something else wrong with your form. I would suggest you print the errors when is_valid fails and see what's missing.
When running the following jinja code, I only get "Column info" printed. Why the index does not appears ?
{% for field in columns_form %}
{% if 'title_' in field.name %}
<td>Column {{ loop.index }} info</td>
{% endif %}
{% endfor %}
It sounds like the template is being treated as a Django template, not a Jinja template.
Using {{ loop.index }} should work in a Jinja template, but wouldn't work in a Django template, where you would use {{ forloop.counter }} instead.
In case {{ loop.index }} does not work in the latest version, a workaround is to zip the columns_form and range(0, len(columns_form)+1) in the python file as
columns_form_idx = zip(columns_form, range(0, len(columns_form)+1))
In the template file,
{% for field, idx in columns_form_idx %}
{% if 'title_' in field.name %}
<td>Column {{ idx }} info</td>
{% endif %}
{% endfor %}
I'm trying to iterate through a FormField in a Formfield, which are both part of a FieldList.
In my views.py I'm calling for the mainForm, the template iterates successfully through the FormField subForm. However, when I can't get the iteration through the subSubForm to work. Those fields never appear in the browser.
Formcode:
class subSubForm(Form):
step = IntegerField("step", validators=[NumberRange(min=0, max=99)])
description = TextField("Description")
information = TextAreaField("Information Exchanged")])
class subForm(Form):
name = TextField("Description")
step = FieldList(FormField(subSubForm), min_entries=1)
class mainForm(Form):
sub_form = FieldList(FormField(subForm), min_entries=1)
And the Jinja2 template:
{% for sub_form in form.sub_form %}
{{ sub_form.form.name(placeholder='Scenario Title') }}
{% for error in name %}
{{error}}
{% endfor %}
{% for step in form.sub_form %}
{{ step.form.id(placeholder='#') }}
{{ step.form.description(placeholder='description') }}
{{ step.form.information(placeholder='info xch') }}
{% endfor %}
{% endfor %}
{% endfor %}
The above only shows the sub_form.form.name-field.
How can I do nested iteration so that the step fields are also shown?
It looks like you made mistake in second nested loop in your template. Here is its fixed version:
{% for sub_form in form.sub_form %}
{{ sub_form.form.name(placeholder='Scenario Title') }}
{% for error in name %}
{{error}}
{% endfor %}
{% for step in sub_form.step %}
{{ step.form.step(placeholder='#') }}
{{ step.form.description(placeholder='description') }}
{{ step.form.information(placeholder='info xch') }}
{% endfor %}
{% endfor %}
This renders to:
<input id="sub_form-0-name" name="sub_form-0-name" placeholder="Scenario Title" type="text" value="">
<input id="sub_form-0-step-0-step" name="sub_form-0-step-0-step" placeholder="#" type="text" value="">
<input id="sub_form-0-step-0-description" name="sub_form-0-step-0-description" placeholder="description" type="text" value="">
<textarea id="sub_form-0-step-0-information" name="sub_form-0-step-0-information" placeholder="info xch"></textarea>