Is it possible to render each checkbox individually, instead of it having to clump all the checkboxes together in a list, as it does by default? Something like
{{ myform.cbmultiple.0 }}
To render just the first checkbox? Actually the 0 would have to be a variable so I can loop...
The reason I'm asking is because I want to display these checkboxes in a rather complicated way, so the default widget doesn't work for me. I also don't really want to override the widget because it's much easier to render it using the template syntax than in python code, plus, that's a lot of work for a one-off.
No you can't do that because the whole HTML is generated by the widget's render method at once. You could only create your own widget class and override the render method so that it produces the HTML in a fashion that suits your purpose!
There is a way to render the CheckboxSelectMultiple() manually in the template so you can do what you want with it.
Take a look at this post for details.
The solution could be something like this:
<table>
<thead>
<tr>
<td> </td>
<td>V</td>
<td>S</td>
</tr>
</thead>
{% for pk, choice in form.options.field.widget.choices %}
<tr>
<td>{{ choice }}</td>
<td><label for="id_options_{{ forloop.counter0 }}"><input {% for option in app.options.all %}{% if option.pk == pk %}checked="checked"{% endif %}{% endfor %} type="checkbox" id="id_options_{{ forloop.counter0 }}" value="{{ pk }}" name="options" /></label></td>
</tr>
{% endfor %}
</table>
Related
Scenario:I want to make a page for available slots for booking ,for which I want print time slot for 1 hour that is from i To i+1 and color it according to the availability.
I am newbie to django and can't figure out the way to make a calendar and that is why I am printing time values in HTML template.
Is there any other way to make this page and also is printing 'i+1' possible.
Views.py
#login_required
def slots(request):
k={ 'pro':range(8,17)
}
return render(request, 'login/slots.html',k)
slots.html
{% for i in pro %}
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td id="demo">{{ i }} to {{ i+1 }}</td>
</tr>
{% endfor %}
I know this might be a silly question but I am not able to figure it out,
any help is appreciated.
There is a built-in filter in Django template called add. You can add something to a value in templates using {{ value|add:"2" }}.
In this case specifically, try:
<td id="demo">{{ i }} to {{ i|add:"1" }}</td>
I am learning how to implement django pagination.
I want to let user save all changes (the whole form no matter which pagination )when he/she clicks the save-all button. However, when using forloop.counter0, the django will render duplicate forloop counter.
How can I generate continuous unique id from 0 to n-1 so that at views.py, the views can recognize every items? Thanks!
{% for thing in things %}
<tr id="tr-{{ thing.id }}">
<td style="display:none"><input type="text" name="hidden-id-{{ forloop.counter0 }}" value="{{ thing.id }}"></td>
</tr>
{% endfor %}
Is there existing any methods like plussing the pagecounter and the forloop counter?
After some trials-and-errors:
I've found that django by default (or maybe always) don't let us save things across all page(pagination).
To generate unique IDs across all pages, we can use the |add filter together with the start_index attribute which is autogenerated by django.
{% for thing in things %}
<tr id="tr-{{ thing.id }}">
<td style="display:none"><input type="text" name="hidden-id-{{ things.start_index |add:forloop.counter0 }}" value="{{ thing.id }}"></td>
</tr>
{% endfor %}
i need some help in following situation.
After a successful searching for a user, i want to show the users information. It's a m2m flied, but I'm getting all objects from this model.
I do not know, how to filter users information.
Got this template:
{% for player in players %}
<tr>
<td>{{ player.last_name }} <span class="text-muted">({{ player.first_name }})</span></td>
<td>{{ player.gender }}</td>
<td>
{% for choice in search.league %}
<div class="">
{{ choice }}
</div>
{% endfor %}
</td>
This way it is showing all objects in League.
The field league is a M2M field from player.
I have this in my forms.Form
league = forms.ModelMultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, queryset=League.objects.all())
I realize that I must also send the users-information from the view, but I do not know how.
Thanks for helping.
I would say that you want to use something like {% for choice in player.league.all %} in your template and use it in your for-loop. You can find more information about this here: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.related_name
Is there a way to access a specific form from an inline formset, in the template, without hardcoding the index?
I know that the usual way to iterate through a formset is to do something like:
{% for form in formset %}
{{ form }}
{% endfor %}
But due to some details on the template (i have multiple formsets, that should be displayed side by side on a table, inside another for), it would be better if i could access each form by its index. I can do this by hardcoding the index, like {{ formset.0 }}, but since i'm iterating in the template, the ideal would be to get the form by the forloop.counter, so that i could do something like
{% for field in fields %}
<tr>
<td>{{ field }}</td>
<td>{{ formset1.[forloop.counter0] }}</td>
<td>{{ formset2.[forloop.counter0] }}</td>
</tr>
{% endfor %}
Is there a way to achive this?
Custom indexing isn't possible inside template.
You can achieve the same result by creating your own filter. See the following snippet:
http://djangosnippets.org/snippets/2740/
Alright, here's my situation. I've got an array of generic objects that I'm iterating over in a django template. Those objects have a number of subclasses and I want to figure out in the template which subclass I'm dealing with. Is this possible? Advisable?
The code might look something along the lines of (where the if statements include some imaginary syntax):
<table>
<tr>
<th>name</th>
<th>home</th>
</tr>
{% for beer in fridge %}
<tr>
<td>
{{ beer.name }}
</td>
<td>
{% if beer is instance of domestic %}US of A{% endif %}
{% if beer is instance of import %}Somewhere else{% endif %}
</td>
</tr>
{% endfor %}
</table>
This is an old question, but FWIW you can do this with a template filter.
#register.filter
def classname(obj):
return obj.__class__.__name__
Then in your template you can do:
{% with beer|classname as modelclass %}
{% if modelclass == "Domestic" %}US of A
{% elif modelclass == "Import" %}Somewhere else
{% endif %}
{% endwith %}
You'll have to do it via some sort of method. Why not just write a method like display_location() or something on the model itself and have it return the string which gets rendered there? Then you could just put {{ beer.display_location }} in your template.
Or if you want to go really crazy, write a custom template tag that does what you want, but that's much more work.