set attribute of a row in django-tables2 to record.id - django-tables2

I am trying to set an attribute of a row in django-tables2 to id of a current record.
To do so I have modified one row in table.html template in such a way:
<tr row_id="{{ record.id }}" class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}">
But this gives me empty row_id="" in html result. What is wrong?

The tables2 row object is not the record.. but the record is within it. So you can do:
<tr id="row_{{ row.record.id }}" class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}">
Or using a data property
<tr data-record-id="row_{{ row.record.id }}" class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}">
This definitely works.. I am using it.
Also look at the (last) answer here: django-tables2 specify different properties for different rows which suggests using a custom render method to add some html to a cell within each row. That way you don't have to maintain a custom tables2 template. Then with help from jquery it's easy enough to find the element containing the id.. if the user clicks on the row, or whatever it is that you'd like to do...

The default name for the for variable for django-tables2 table.html (as we see at https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html#L25) is row (and not record which is used for TemplateColumn).
You've not shared your table.html template however I think that this probably why you see an empty row_id. So can you try changing row_id="{{ record.id }}" to row_id="{{ row.id }}" ?

Related

Drupal 8 Twig looping through a paragraph EntityRevisionItemList

i have a Node which has a field node_field (entity reference revision) which points to a paragraph (can be added 3 times). That paragraph has only 1 field in it, which itself is a entity reference field that points to a node bundle.
I'm trying to loop through each paragraph and each node inside so i can render the entire node (not individual fields), i only want the nodes, don't need to render the paragraph.
Here's my approach in the node.twig.html via the content variable and it works, i get the desired result.
{% if content.my_field|render %}
{% for key, item in content.my_field if key|first != '#' %}
<div class="col-lg-4 col-md-6">
{{ item }}
</div>
{% endfor %}
{% endif %}
Now i want to do the exact same thing but use the node variable instead of content but i just can't seem to get it to print. Any ideas?
Realy it's depend of the content that you have into the item. If you have devel installed, you could use kint to see the difference between the content of both, the differents variables, etc...
Depends of the element where you are iterating you can render each elements, other times you need get the value of each item (item.getValue()[0]).
I hope that my answer could be enought.

How can i add "if condition" around the anchor to check the field value empty or not?

I need to check if anchor URL added from the CMS or not?
Actually didn't find any useful link yet, but I need help that I've created a Email widget and when I drag and drop that I need to add anchor if url added in the field from the Kentico Email CMS if didn't add anything in the anchor field it should not add anchor html.
Please help!
Thanks in Advance.
Here is the Anchor code in the widget:
<td>
Read More
</td>
{% if (Header_Circulation_Link == null) { %}
<div>Its null</div>
{% } else { %}
<div>its not null</div>
{% } %}
Equality checks support all available object types, with the following
special rules:
Empty strings are equal to null
See macro docs for more

Jekyll – Check if page belongs to a collection

Is there a way to check if a current page belongs to a collection? If so, can I check what’s the label of the collection?
Background story:
What I’m trying to achieve is to display a different layout for all pages that belong to a collection. With this new layout I want to display a side navigation (some sort of „See also”) with listing of all pages in the collection that the current site belongs to.
In case my question turns out to be dumb – forgive me, I’m new to jekyll and I really tried to find out the answer by myself.
page.collection returns the label of the collection to which the document belongs. So I guess you want to do something like:
{% if page.collection == 'COLLECTION_LABEL' %}
NEW LAYOUT
{% else %}
OLD LAYOUT
{% endif %}
To access the pages in a collection, you can use the label with site.COLLECTION_LABEL or site[COLLECTION_LABEL] (you need [] if it is a variable). Something like:
{% for page in site[page.collection] %}
{{ page.title }}
{% endfor %}
Any collection document has a page.collection variable which is the collection label.
But, if you want to associate a specific layout to a collection, you can use default configuration :
defaults:
-
scope:
type: mycollection
values:
layout: a_custom_layout
anyvar: a_custom_var

Django: One template or two templates for different CSS?

I am making a website in Django for an online multiple-choice test. For each question, the question text is displayed on the webpage, together with a set of radio buttons for the possible answers, and a submit button. The user chooses on of the answers, and presses the button. Then, I want to give feedback to the user: A green "Correct", or a red "Incorrect", together with a button to retrieve the next question.
What is the best way to do this in Django? I could have two templates: one for the question, and one for the feedback, and each with one associated view function. For the question, I would pass variables for the question text, together with the text for the possible answers. This seems to work well. However, for the feedback, I would pass the text "Correct" or "Incorrect" as a variable, but how do I now change the CSS to set the text to be green if "Correct", and red if "Incorrect"? Would it be more sensible to have a separate template for the "Correct" case, and the "Incorrect" case?
Thank you.
The Django view should pass whether the answer is correct or not, i.e. a boolean.
Suppose the boolean variable in the template context is called correct; your template code could be something along these lines:
{% if correct %}
// correct html markup in here
<span class="correct">Correct!</span>
{% else %}
// incorrect html markup in here
<span class="incorrect">Woops, not correct!</span>
{% endif %}
The above should all go in the same template, i.e. one template only.

How to remove inputs/fields created by a form in Django

I have a simple form for uploading a profile picture and then a thumbnail image showing what has been uploaded. The HTML code is:
<form action="{% url 'base-welcome' %}" enctype="multipart/form-data" method="post">
{% csrf_token %}
<ul>
<li><span>Upload A Profile Picture:</span> <img src="{{ user.profile.get_thumbnail }}">{{ profile_form.picture }}</li> , then closing elements, etc..
The problem is that the form is creating unwanted elements in the "li". See the following image:
The "currently" and "change" have been created. I've already hidden a checkbox that was created using display: none. I can't really do that with these elements because they are just text that was generated.
I think this is a problem with the models.py/views.py pages, but I may be wrong. Just looking for a simple HTML/CSS fix. Thank you so much!!
It looks like picture field is using ClearableFileInput widget. To use standard FileInput widget (without these extra words and checkbox), do just in your form:
picture = forms.ImageField(upload_to='/path/', widget=forms.FileInput)
In that case you just have to render your forms manually :)
You could either write your own helpers - I can see this as ie. custom filters: {{ form.field|as_file_input_field }} or you could use some external helpers like: http://djangopackages.com/grids/g/forms/
I would still suggest writing your own helpers just to learn how django forms renderer works :)
You could also take a look here for some samples of how we tried to solved this problem while before: https://github.com/efabryka/django-template_widgets :)
This is how the ImageField default widget is rendered - maybe it will be worth your while: https://github.com/django/django/blob/master/django/forms/widgets.py#L298
Are you just wanting to hide some text? I haven't touched HTML/CSS in a while but can't you just put the text inside a div or a span and then hide that?
set color: white; for that element, then all text will be "invisible" lol.

Resources