I have the following code in my template (please note the if statement):
{% for base in bases %}
<label class="checkbox">
<input name="base" value={{ base.id }} type="checkbox"
{% if base.id in selected_bases %}checked="checked" {% endif %}/>
<span>{{ base.name }}</span>
</label>
{% endfor %}
The selected_bases variable is a list of unicoded strings: [u'3', u'1', u'5'].
base.id is an integer.
How can I make them the same type so that if statement does what I need it to?
I don't know if this work, but try this:
{% if value|stringformat:"d" in selected_bases %}
You should probably do this in the view instead, but you can pipe the list values through the add filter, which does type coercion - or pipe the ints to slugify, which will do the reverse. More information here.
Related
I have a simple form which uses a SessionWizardView to spread it over a number of pages. Below is an example of one of the questions.
first_name = forms.CharField(max_length=100, label='What is your first name?', error_messages={'required': 'Please enter your first name'})
Which renders out as
<label for="id_0-first_name">What is your first Name?</label>
<ul class="errorlist">
<li>Please enter your first name</li>
</ul>
<input id="id_0-first_name" maxlength="100" name="0-first_name" type="text" />
Can anyone tell me hwo to change the error output so that it is in <p> Paragraph </p> format rather than <li> List item </li> format?
I am using Django 1.6.2
You'll have to create a class that does renders the HTML as you would want it. See the docs here.
The example from the docs:
from django.forms.util import ErrorList
class DivErrorList(ErrorList):
def __unicode__(self):
return self.as_divs()
def as_divs(self):
if not self: return u''
return u'<div class="errorlist">%s</div>' % ''.join([u'<div class="error">%s</div>' % e for e in self])
f = ContactForm(data, auto_id=False, error_class=DivErrorList)
f.as_p()
You can do as #schillingt suggested and create your own error list class.
Or, if you want to handle this in your template, you can use something like:
<form method="post" action="/some-view/">
... other fields, etc. omitted ...
<!-- The label and form field -->
{{ form.first_name.label_tag }}
{{ form.first_name }}
<!-- Output any errors -->
{% for error in form.first_name.errors %}
<p>{{ error }}</p>
{% endfor %}
... other fields, etc. omitted ...
<button type="submit">Submit</button>
</form>
Update
In order to do this in a cleanly repeatable way, make a template named form-field.html:
{{ field.label_tag }}
{{ field }}
<!-- Output any errors -->
{% for error in field.errors %}
<p>{{ error }}</p>
{% endfor %}
Then, update your main template:
<form method="post" action="/some-view/">
... other fields, etc. omitted ...
{% with field=form.first_name %}
{% include "form-field.html" %}
{% endwith %}
... other fields, etc. omitted ...
<button type="submit">Submit</button>
</form>
You can then make updates to the single form-field.html template and update all of your forms, and it makes your main template a bit simpler
In the template I get the whole DropDown correctly shown with something like this:
{{form.deal_type}}
But what if I wanted just the text of the selected dropdown shown?
This shows me just a foreignkey.
{{form.deal_type.value}}
I don't know why you want to do this exactly, but try this.
TO LOOP:
{% for value, text in form.deal_type.field.choices %}
{{ value }}: {{ text }}
{% if value == form.deal_type.value %}
<strong>{{ text }}</strong> <!-- THIS IS THE SELECTED ONE... -->
{% endif %}
{% endfor %}
EDIT:
I meant the above code as an illustration, not that you should use it verbatim. This code will do more like what you want.
{{ form.deal_type.label_tag }}
{% for value, text in form.deal_type.field.choices %}
{% if value == form.deal_type.value %}
{{ text }}
<input type="hidden" name="deal_type" value="{{ value }}" />
{% endif %}
{% endfor %}
I had a similar issue. To solve it I just passed the value to the template directly from the view. So in your view you presumably have something in the way of
data = {'form' :form,}
return render_to_response('destination.html', data, context_instance = RequestContext)
In data you are passing the form that includes deal_type. Add to
data a variable deal_type set equal to Object.deal_type.display_value with
data = {'form' :form,}
if Object.deal_type: data['deal_type'] = Object.deal_type.display_value
return render_to_response('destination.html', data, context_instance = RequestContext)
Then on your template you can just use
{% if condition_to_show_just_text %}
{{deal_type}} {{form.deal_type.as_hidden}}
{% else %}
{{form.deal_type}}
{% endif %}
It may be insiginificant in this case, but it seemed to me that if the list was long, iterating with the for loop on the template would be less efficient than pulling directly from the object
I am using django-transmeta for translation. In the below code, {{ obj.description }} returns the description in the current language of django. What I need is, getting the obj.description_[lang_code]. How can I get it?
{% for lang in languages.all %}
<div id='{{ lang.code }}'>
<input type="text" name="description-{{lang.code}}" value='{{ obj.description }}'/>
</div>
{% endfor %}
As I understood from your comment you want to get description of specific language in for loop?
then simply write a custom filter like in this way
{{ obj|get_lang_info:lang.code }}
here get_lang_info is custom filter.
Is it possible to reset the forloop.counter object back to zero?
I'm using it to spit out objects, but when the object type changes I want to zero it, so that I can count to 5 objects and output an end of row object (with no right padding) as the fifth and only fifth object in that type.
And if I've got 5 objects, the first two are type a and the next 3 are type b, the fifth one will always get the smaller padding, never mind that its only the third on the line.
Here is my template code:
{% for project in projects %}
{% ifchanged project.proj_type %}
{% forloop.counter=="1" %}
</div>
<div class="span-4 prepend-top">
<h5 class="right" >{{ project.proj_type }}</h5>
</div>
<div class="span-19 append-1 last" id="row-of-projects">
{% endifchanged %}
{% if forloop.counter|divisibleby:"4" %}
<div class="span-4 append-1 prepend-top last" id="project">
{% else %}
<div class="span-4 append-1 prepend-top" id="project">
{% endif %}
<p class="project-name">{{ project.name }}</p>
<a href="/gallery/{{ project.slug }}/" ><img src="/media/pa/photographs/{{ project.get_photograph }}-t.jpg" alt="{{ project.name }}" /></a>
</div id="project">
{% endfor %}
It sounds like you should be regrouping objects based on type, and doing the padding in the inner loop.
I'm not quite sure that I understand what you need.
You can use the regroup tag to display your objects according to type.
And you can use class="{% cycle 'normal' 'normal' 'normal' 'normal' 'special' %}" to style every fifth element differently.
I'm rendering a form. I would like to put a mark next to all the fields that must be filled in. Simple stuff usually... but I've no idea how to access that information!
{% if field.required %}REQUIRED!!{% endif %}
doesn't bring any love...
{% if field.field.required %}
From this snippet
I usually write out the html for each of my form elements, so I have a little more control over each one. Then you can add an * for required or REQUIRED!!.
<p><label>Title: *<br />
{% if form.title.errors %}<ul class="errorlist">{{ form.title.errors }}</ul>{% endif %}
{{form.title}}
</label></p>
<p><label>Category:<br />
{% if form.category.errors %}<ul class="errorlist">{{ form.category.errors }}</ul>{% endif %}
{{form.category}}
</label></p>