Why url() give me no values when calling inside included template? - django

Here is the code:
#list.html
{% if intentions %}
<table class="table">
{% for i in intentions %}
{% url 'intentions.views.show' i.id as iUrl %}
<tr>
<td width="20%">
<span class="label label-success">x prayers</span>
</td>
<td><h3>{{ i }}</h3></td>
</tr>
{% endfor %}
</table>
{% else %}
<p>
No intentions are available.
</p>
{% endif %}
If this code is inside index.html everything is ok. If I try to move this code to separate file list.html and include in index.html like:
{% include "list.html" with intentions=entries %}
I have no values in url attribute of <a> tag. Could someone give me answer why?

Your list.html template might be missing
{% load url from future %}
Because you are using the 'as' syntax, the url tag fails silently.

Related

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

Django 2.2 How to disable checkboxes in list view

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.

get a List of List object and how to handle it in html with django

index.html
{% block content %}
{{playerList}}
{% for player in playerList %}
{{player.value.indexOf(0)}}
{% empty %}
<tr>
<td class="bg-light text-center font-italic" colspan="3">You haven't registered any players yet.</td>
</tr>
{% endfor %}
{% endblock %}
playerList is a list of lists, which is returned from views.py file.
i.e playerList=[["chamo",'1'],["gir",'2'],["frt",'2']]
if I want to get the "chamo" from playerList, how should I write it in html file?
You can just do {{ player.0 }}.

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>

How to use date based views in Django

It maybe a little naive question but I have been trying to understand how I can use the new Date Based views in django, but without an example, I am at a dead end. What I want to do is to show all my blog entries on a page (with pagination) and in the side navigation I want to show the archiving done according to year and month.
What I want is very basic and can be seen in the picture attached below.
If somebody can provide me an example then it would be really great. I can handle the templates, but just need to know how to use the class based generic views. I haven't really really used much if generic views.
The simplest example:
views.py
from django.views.generic.dates import MonthArchiveView
from myapp.models import Article
urlpatterns = patterns('',
url(r'^articles/monthly/$',MonthArchiveView.as_view(
model=Article,
paginate_by=12,
date_field='publish_date',
template_name='archive_templates/monthly.html',
),name="monthly"),
)
You need to call the view with a correct day and month, otherwise you'll get an exception. Default arguments are year in Y format, and month in b (lowercase short month name).
Example call articles/monthly/?year=2012&month=feb
Here is a sample archive_templates/monthly.html you can use.
I am using css classes from the excellent twitter bootstrap framework. Highly recommended!
This snippet goes through the available months:
Archive for {{ month|date:"F" }} {{ month.year }}<br />
<div class="pagination pull-left">
<ul>
{% if previous_month %}
<li class="prev">
<a href="{% url monthly %}?year={{ previous_month|date:"Y" }}&month={{ previous_month|date:"b" }}">
← {{ previous_month|date:"M Y" }}
</a>
</li>
{% endif %}
{% if next_month %}
<li class="next">
<a href="{% url monthly %}?year={{ next_month|date:"Y" }}&month={{ next_month|date:"b" }}">
{{ next_month|date:"M Y" }} →</a>
</li>
{% endif %}
</ul>
</div>
{% endif %}
This snippet does the pagination:
{% if is_paginated %}
<div class="pagination pull-right">
<ul>
<li class="{% if page_obj.has_previous %}prev {% else %} prev disabled {% endif %}">
←</li>
<li class="disabled"><strong>{{ page_obj.number }} of {{ paginator.num_pages }}</strong></li>
<li class="{% if page_obj.has_next %}next{% else %} next disabled {% endif %}">
→
</li>
</ul>
</div>
{% endif %}
The actual list of objects is very simple to iterate over:
<table class="zebra-striped" width="100%">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Author</th>
<th>Published On</th>
</tr>
</thead>
<tbody>
{% for obj in object_list %}
<tr>
<th>{{ forloop.counter }}</th>
<td>{{ obj.title }}</td>
<td>{{ obj.author }}</td>
<td>{{ obj.publish_date|date:"d/m/Y" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
From here you should be able to figure out how to develop your archive menu.