Django 2.2 How to disable checkboxes in list view - django

Django 2.2
I have a list view controlled by admin.py class. No custom template, all default. I can control what fields from the table should be shown in the view with this:
fields = ('myfield1','myfield2', ...).
Each row in the list table has a checkbox in the first column, the source looks like this:
<td class="action-checkbox">
<input type="checkbox" name="_selected_action" value="123" class="action-select">
</td>
My questions are:
How to disable those checkboxes (preferably, from Django code, without introducing a custom template) ?
Can it be done for SOME of the checkboxes (let's say I have a list of pk ids for the rows I don't want to see checkboxes.)

You can delete Items with those CheckBoxes, but if you want to customize your own admin page to override it
You can use this doc https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#admin-overriding-templates

This question is 2 years old now, but for the case someone still needs it, the following code works to overwrite the change-list_results.html:
{% load i18n static %}
{% if result_hidden_fields %}
<div class="hiddenfields">{# DIV for HTML validation #}
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
</div>
{% endif %}
{% if results %}
<div class="results">
<table id="result_list">
<thead>
<tr>
{% for header in result_headers %}
{% if "checkbox" in header.text %}
{% else %}
<th scope="col" {{ header.class_attrib }}>
{% if header.sortable %}
{% if header.sort_priority > 0 %}
<div class="sortoptions">
<a class="sortremove" href="{{ header.url_remove }}" title="{% translate "Remove from sorting" %}"></a>
{% if num_sorted_fields > 1 %}<span class="sortpriority" title="{% blocktranslate with priority_number=header.sort_priority %}Sorting priority: {{ priority_number }}{% endblocktranslate %}">{{ header.sort_priority }}</span>{% endif %}
</div>
{% endif %}
{% endif %}
<div class="text">{% if header.sortable %}{{ header.text|capfirst }}{% else %}<span>{{ header.text|capfirst }}</span>{% endif %}</div>
<div class="clear"></div>
{% endif %}
</th>{% endfor %}
</tr>
</thead>
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr>
{% for item in result %}
{% if "_selected_action" in item %}
{% else %}
{{ item }}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
It stops stops the output of the for loops if theres a checkbox in there. --> It just removes the checkboxes.

Related

DJANGO - access form data in javascript in template

How can I access form data in javascript in template?
I have a bootstrap 4 tabbed interface.
Each tab renders a dynamic django form.
Each form has dynamic 'command' buttons.
All form field and button data is coming from a json file.
So something like this...
The command buttons are handled by ajax, which basically calls a url based on the button clicked.
All of the tab forms are rendered by a single template.
How can I access the form data (different for each tab) in the javascript function?
( In the js function, look at 'SAVE FORM DATA HERE' and 'PASS FORM DATA HERE' )
template
{% load static %}
<table class="" id="tabCmdTbl">
<div class="btn-group">
<tr>
{% for btn in btns %}
<td width="125px">
{% if btn.btnText != "" %}
{% if btn.btnText == "End" %}
{% comment %}
<!-- Handle the exit button, its an anchor, all the other tabs are ajax -->
{% endcomment %}
<a type="button" class="btn btn-sm btn-outline-secondary w-100" target="_blank" href="/frontOffice">End</a>
{% else %}
<button type="button" class="btn btn-sm btn-outline-secondary w-100" onclick="tabBtn('{{btn.btnCmd}}')">
{{ btn.btnText }}
</button>
{% endif %}
{% endif %}
</td>
{% endfor %}
</tr>
</div>
</table>
<div id="tabloading" style="height:50px">
<p><img src="{% static 'FhHRx.gif' %}" /> Please Wait</p>
</div>
<hr>
<table id="tabDataTbl" border="1px" style="background-color:rgb(225, 229, 238)" >
{% for field in form.visible_fields %}
{% ifchanged field.field.row %} {% comment %} if the row changed {% endcomment %}
{% if field.field.row != 1 %} </tr> {% endif %} {% comment %} unless its the first row, end the row {% endcomment %}
<tr> {% comment %} rowchanged, not first row, start new row {% endcomment %}
{% endifchanged %}
{% ifchanged field.field.col %}
{% if field.field.col != 1 %} </td> {% endif %}
<td style="padding:5px">
{% else %}
{% ifchanged field.field.row %}
{% if field.field.row != 1 %} <td style="padding:5px"> {% endif %}
{% endifchanged %}
{% endifchanged %}
{{ field.label }}</td><td> {{ field }} </td>
{% endfor %}
</table>
<script>
$('#tabloading').hide();
function tabBtn(datap) {
$('#tabloading').show();
// alert(datap);
$.ajaxSetup({
data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
});
if (datap.includes("Save")) {
------ SAVE FORM DATA HERE ------
alert('x')
}
$.ajax({url:datap, //url,
data: { provider_code: '{{form.provider_code.value}}',
patient_number: '{{form.patient_number.value}}',
account_number: '{{form.account_number.value}}',
insurance_number: '{{form.insurance_number.value}}',
------ PASS FORM DATA HERE ------
},
type:"POST",
success:function(tabdata){
// alert(tabdata);
$('#tabloading').hide();
$("#tab-content").html(tabdata);
}
});
}
</script>

overriding Django change_list_results

I am customizing my admin panel a little and want to add an additional column to it, so as per the docs I have to override the change_form_results.html file but how do I do it? all I see is this code in it
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td> </tr>
{% endif %}
<tr>{% for item in result %}
{{ item }}
{% endfor %}</tr>
{% endfor %}
</tbody>
what change am i suppose to make to above code

Displaying label on first form of Django formset only

This is a super straightforward question but I can't seem to find any concise answer it. I have a Django formset that displays different tags associated with an object. Here is the form:
class TagForm(forms.Form):
def __init__(self, *args, **kwargs):
tags = kwargs.pop('tags')
super(TagForm, self).__init__(*args, **kwargs)
self.fields['tags'] = forms.ChoiceField(choices=[(tag, tag) for tag in tags], label="Tags")
I'm rendering the formset using the following code:
<li class="list-group-item">
<ul class="list-inline" id="tag-group">
{{ tag_formset.management_form }}
{% for tag_form in tag_formset %}
<li class="list-inline-item">
{{ tag_form.tags.label_tag }}
{{ tag_form.tags }}
</li>
{% endfor %}
</ul>
</li>
My problem is that this creates a label for each tag. Since this is an inline list, I'd only like to display the label prior to the first tag (and no others). I can't find any straightforward way to do this (without modifying the for loop with explicit logic checking if it is the first form being rendered). I optimistically tried to modify my rendering code to the following:
<li class="list-group-item">
<ul class="list-inline" id="tag-group">
{{ tag_formset.management_form }}
{{ tag_form.empty_form.label_tag }}
{% for tag_form in tag_formset %}
<li class="list-inline-item">
{{ tag_form.tags }}
</li>
{% endfor %}
</ul>
</li>
but this didn't display any labels at all. Is there an idiomatic way to only display the form label prior to the first form in a formset?
The code below is working for me. The main idea is simple. Make a Table and in the headers put the tags. and in the table body put only the data. Try it and let me know.
{% for f1 in formset %}
{{ f1.management_form|crispy }}
{% crispy f1 %}
{% endcomment %}
<table{% if form_id %} id="{{ form_id }}_table" {% endif%} class="table table-striped table-condensed">
<thead>
{% if formset.readonly and not formset.queryset.exists %}
{% else %}
<tr>
<td>
</td>
{% for field in formset.forms.0 %}
{% if field.label and not field.is_hidden %}
<th for="{{ field.auto_id }}"
class="control-label {% if field.field.required %}requiredField{% endif %}">
{{ field.label|safe }}{% if field.field.required %}<span
class="asteriskField">*</span>{% endif %}
</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
</thead>
<tbody>
{% comment %} <tr class="hidden empty-form">
{% for field in formset.empty_form %}
{% include 'bootstrap/field.html' with tag="td" form_show_labels=False %}
{% endfor %}
</tr> {% endcomment %}
{% for form2 in formset2 %}
{% if form2_show_errors and not form2.is_extra %}
{% include "bootstrap/errors.html" %}
{% endif %}
<tr>
<td>
<a class="btn btn-info pull-right" {% comment %}
href="{% url 'set_final' formfs.pk %}/?next={% url 'update-well-view' form.pk %}">
{% endcomment %}
href="{% url 'Scouts-home' %}"> Set Final
</a>
</td>
{% for field in form2 %}
{% include 'bootstrap/field.html' with tag="td" form2_show_labels=False %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>

No named cycles in template. 'row1,row2' is not defined

In my django inline formset, form html:
{% block body %}
<h2>Profile</h2>
<hr>
<div class="col-md-4">
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<table class="table">
{{ familymembers.management_form }}
{% for form in familymembers.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1,row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" value="Save"/> back to the list
</form>
</div>
{% endblock %}
When I tried to open form it gives
TemplateSyntaxError at /profile/add/
No named cycles in template. 'row1,row2' is not defined
How could I avoid this error?
That's not how you use that tag, as the docs show. The values should be separated by spaces, not commas, and if they are literal strings they should be in quotes.
{% cycle "row1" "row2" %}
If you still get an error, you can try:
class="{% cycle 'row1' 'row2' %} formset_row"

Django - customize result list - add element ID

I would like to override default Admin Panel result list (change_list_results.html) to add ID for each row in <tr>
Default is:
<tbody>
{% for result in results %}
{% if result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}">{% for item in result %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
I would like to have:
<tbody>
{% for result in results %}
{% if result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}" id="{{ ID }}">{% for item in result %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
What should i put into {{ ID }} to get element ID?
Unfortunately, the 'items' context variable does not hold instance data, it's just a list with html for each column. To achieve what you need you must search for the equivalent item in the 'cl.result_list' context variable, based on the forloop counter:
<tr class="{% cycle 'row1' 'row2' %}" id="{% with i=forloop.counter0|stringformat:"s"|add:":" %}{% with items=cl.result_list|slice:i %}{{ items.0.pk }}{% endwith %}{% endwith %}">{% for item in result %}{{ item }}{% endfor %}</tr>
Alternatively, if you don't like all these 'with' tags you could create a custom template to get the item from "cl.result_list" directly from the counter index.
I'd guess {{ result.form.instance.id }} judging by the fact there is a result.form attached to a list of results.
Just deductive reasoning. Let me know if it's wrong.