I struggling to solve the following:
I am customizing django tabular inline template which contains several fields.
I have a condition
{% if field.field.name == 'productid' %} ... {% endif %}
However there are two fields that have the ... condition applied which is "productid" and "distributionid price productid" - both contain the productid word. However, I want only the former to have it. How can I make this condition more strict?
Any help will be much appreciated.
EDIT:
html file:
{% if field.field.name == 'productid' %}
<input type="text" name="PN" id="PN" placeholder="PN:"/>
{% endif %}
{% if field.field.name != 'productid' %}
<td class="field-{{ field.field.name }}"
data-id="{{ field.field.id }}" data-type="id">
{% if field.is_readonly %}
<p>{{ field.contents }}</p>
{% else %}
{{ field.field.errors.as_ul }}
{{ field.field }}
{% endif %}
(rerults in )
Your if condition is outside the field td.
Just put it with field.field tag
{% if field.is_readonly or not field.field.is_hidden %}
<td{% if field.field.name %} class="field-{{ field.field.name }}"{% endif %}>
{% if field.is_readonly %}
<p>{{ field.contents }}</p>
{% else %}
{{ field.field.errors.as_ul }}
{{ field.field }}
{% if field.field.name == 'productid' %}
<input type="text" name="PN" id="PN" placeholder="PN:"/>
{% endif %}
{% endif %}
</td>
{% endif %}
Related
How would I use a custom form to display inside the session wizard so when it goes through each step it displays the html for each form inside the signup.html.
createUser.html
{% extends 'base.html' %}
{% block title %}Create User{% endblock %}
{% block content %}
<form method="POST" action='.' enctype="multipart/form-data">
{% csrf_token %}
<!-- A formwizard needs this form -->
{{ wizard.management_form }}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button>Sign up</button>
<button>Log In</button>
</form>
{% endblock %}
views.py
class UserWizard(SessionWizardView):
template_name = "registration/signup.html"
form_list = [SignUpForm]
def done(self, form_list, **kwargs):
process_data(form_list)
return redirect('home')
signup.html
{% extends 'base.html' %}
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block title %}Sign up{% endblock %}
{% block content %}
<h2>Sign up</h2>
<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 %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<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 am trying to add different block tags inside a for loop but it raise an error
Did you forget to register or load this tag?
But I register it
{% for todo in todo_list %}
{% if todo.complete %}{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
<small class="text-muted">{{todo.content|capfirst}}{% empty %} {% endif %} </small> <hr>
{% endif %} {% endfor %}
Thanks
Looking into your problem, I think you need to try this:
{% for todo in todo_list %}
{% if todo.complete %}
{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
{% if todo.content %}
<small class="text-muted">{{todo.content|capfirst}} </small> <hr>
{% else %}
//do something
{% endif %}
{% endif %}
{% endfor %}
I have a django template where I need to cycle through a set of background colours across two different for loops. The cycle tag seems to be designed to be used either within one for loop or outside a for loop altogether. This is my code:
{% if global_adverts %}
<span style="display:none">{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors %}</span>
{% for advert in global_adverts %}
<div class="{% cycle adcolors %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{% cycle adcolors %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% endwith %}
Is there a way to do this without outputting the first item in the cycle before the first loop and having to hide it with css?
Just add silent
{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors silent %}
It prevents the tag from outputting the value
EDIT AFTER COMMENT
If you need the cycle to continue for each loop just use cycle twice and change the order of the items in the second cycle:
{% if global_adverts %}
{% for advert in global_adverts %}
<div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{% cycle 'advert-pale-blue' 'advert-green' 'advert-blue' 'advert-grey' %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% endwith %}
I have discovered that the following code gives the desired result:
{% if global_adverts %}
{% for advert in global_adverts %}
<div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolours %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{% cycle adcolours %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% endwith %}
I feel that having the first {% cycle %} declaration outside of the loops is cleaner, I wouldn't have expected a declaration inside of conditional loops to work, and here's how to make it work:
{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolour silent %}
{% if global_adverts %}
{% for advert in global_adverts %}
<div class="{{ adcolours }}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% cycle adcolours %}
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{{ adcolours }}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% cycle adcolour %}
{% endfor %}
{% endif %}
{% endwith %}
Since {% cycle adcolour %} is silent, we use {{ adcolour }} to print the current colour. We still need to use the silent {% cycle adcolour %} to step through the colours on each iteration.
I'm trying to follow the guidelines in this answer, but I'm getting stuck with how to edit the template.
The relevant part of my admin.py:
SegmentFormset = forms.models.inlineformset_factory(Division,Segment)
class DivisionForm(forms.ModelForm):
def __init__(self, **kwargs):
super(DivisionForm, self).__init__(**kwargs)
self.segment_formset = SegmentFormset(instance=self.instance, data=self.data,
prefix=self.prefix)
def is_valid(self):
return (super(DivisionForm, self).is_valid() and
self.segment_formset.is_valid())
def save(self, commit=True):
assert commit == True
res = super(DivisionForm, self).save(commit=commit)
self.segment_formset.save()
return res
class DivisionInline(admin.TabularInline):
model = Division
form = DivisionForm
template = 'competitions/admin/tabular.html'
class CompetitionAdmin(VersionAdmin):
inlines = [DivisionInline,]
The relevant part of my template:
{% for fieldset in inline_admin_form %}
{% for line in fieldset %}
{% for field in line %}
<td class="{{ field.field.name }}">
{{ field.field.errors.as_ul }}
{{ field.field }}
</td>
{% endfor %}
{% endfor %}
<td>My segment formset should be here</td>
{% endfor %}
What I can't figure out is how to access the segment formset. I've experimented with all of the variable names and none of them are my DivisionForm. The division formset is the {{fieldset.formset}} variable and that's as far as I've been able to get.
Edit 1:
Actually, the relevant part of the template is a bit longer ;) Putting in more code:
<tbody>
{% for inline_admin_form in inline_admin_formset %}
{% if inline_admin_form.form.non_field_errors %}
<tr><td colspan="{{ inline_admin_form.field_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
{% endif %}
<tr class="{% cycle row1,row2 %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}">
<td class="original">
{% if inline_admin_form.original or inline_admin_form.show_url %}<p>
{% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
{% if inline_admin_form.show_url %}{% trans "View on site" %}{% endif %}
</p>{% endif %}
{% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
{{ inline_admin_form.fk_field.field }}
{% spaceless %}
{% for fieldset in inline_admin_form %}
{% for line in fieldset %}
{% for field in line %}
{% if field.is_hidden %} {{ field.field }} {% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endspaceless %}
</td>
{% for fieldset in inline_admin_form %}
{% for line in fieldset.formset %}
{% for field in line %}
<td class="{{ field.field.name }}">
{{ field.field.errors.as_ul }}
{{ field.field }}
</td>
{% endfor %}
{% endfor %}
<td>My segment formset should be here</td>
{% endfor %}
{% if inline_admin_formset.formset.can_delete %}
<td class="delete">{% if inline_admin_form.original %}{{ inline_admin_form.deletion_field.field }}{% endif %}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
Formsets are like lists, so you can recurse it like
{% for form in fieldset.formset %}
{{ form.as_p }}
{% endfor %}
I would like to modify an admin template in Django.
% cat /Library/Python/2.5/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html
<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}<div class="description">{{ fieldset.description|safe }}</div>{% endif %}
{% for line in fieldset %}
<div class="form-row{% if line.errors %} errors{% endif %} {% for field in line %}{{ field.field.name }} {% endfor %} ">
{{ line.errors }}
{% for field in line %}
<div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}>
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{{ field.label_tag }}{{ field.field }}
{% endif %}
{% if field.field.field.help_text %}<p class="help">{{ field.field.field.help_text|safe }}</p>{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
</fieldset>
What kind of object is field, and more specifically how would I get the name of a field?
field is an instance of AdminField and field.field is an instance of BoundField, so you can reference the fields name with:
{{ field.field.name }}
Once as you start to dive deep into admin customisation, its the only place the documentation is really lacking. that being said, the code is well written and easy to understand if you take the time to research it, IMHO.
There is not many files so take an evening and read through them. In your case, I would start with:
contrib/admin/sites.py
contrib/admin/options.py
contrib/admin/helpers.py
Have you done your research?
After that, I would start poring over the python code that invokes your template. I would imagine that field is from the forms system
Field
A class that is responsible for doing validation, e.g. an EmailField
that makes sure its data is a valid
e-mail address.