How to use date based views in Django - 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.

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.

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>

django template table rows do not collapse

I can not work-out what I am doing wrong with collapsing certain rows of my table.
Can you advice how can I correct it so I can collapse rows marked in red ?
I guess I have placed collapse tag in wrong place or have not used again ? However can I use table in table ?
my template:
{% extends "base.html" %}
{% block content %}
<h3>{% for project in projects %}{{ project.project_name }}{% endfor %}</h3>
<table class="table table-striped table table-bordered table table-hover table table-condensed">
<tr>
<td></td>
<!-- row with zone names -->
{% for z in zones %}
<td style="width:40px" align="center">{{ z.zone_name }}</td>
{% endfor %}
</tr>
<!-- rows with stage names -->
{% for trstage in trstages %}
<tr>
<td style="width:40px"><a data-toggle="collapse" href="#collapse{{ trstage }}">{{ trstage.stage_name }}</a></td>
<!-- row with stage value -->
{% for stage in trstage.stages %}
{% for zone in zones|dictsort:"zone_name" %}
{% if zone == stage.zone %}
<td style="width:40px" align="center">{{ stage.substage_sum.sum }}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
<div id="collapse{{ trstage }}" class="panel-collapse collapse">
<!-- rows with substages names -->
{% for trsubstage in trstage.related_trsubstages %}
<tr>
<td>{{ trsubstage.substage_name }}</td>
<!-- row with substage value -->
{% for substage in trsubstage.substages_related %}
{% for zone in zones %}
{% if zone == substage.stage.zone %}
<td style="width:40px" align="center">{{ substage.substage_value }}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
</div>
{% endfor %}
</table>
{% endblock %}
Below is link to my current template view:
Try adding the id of your trstage to the href and id of the divs. I did something similar in my project and it worked. I hope it works for you too. Like this,
<div id="{{ trstage.id.value }}" class="panel-collapse collapse">
and
<a data-toggle="collapse" href="#{{ trstage.id.value }}">{{ trstage.stage_name }}

How to traverse the two yuan dictionary in the Django template

1.my view have a dict,For example:
category_all = {
u'Python': {
'acticle_info': [
[<Article: python_tump>],[<Article:python_dict>]
],
'article_count': 22
}
}
2.my render is:
return render(request, 'blog/index.html',{'category_all':category_all})
3.i traverse the dict in Django template:
<!-- tabs Nav -->
<ul id="myTab" class="nav nav-tabs" role="tablist">
{% for blog_category in category_all.keys %}
<li role="presentation">{{ blog_category }}</li>
{% endfor %}
</ul>
<!-- Tab panes -->
<div id="myTabContent" class="tab-content">
{% for blog_category,blog_info in category_all.items %}
<div role="tabpanel" class="tab-pane" id="{{ blog_category }}">
<table class="table table-hover">
<tbody>
{% for get_blog_info in blog_info.acticle_info %}
{% for blog_result in get_blog_info %}
<tr class="active">
<td>{{ blog_result.title }}</td>
<td>{{ blog_result.date_publish }}</td>
</tr>
{% endfor get_blog_info %}
{% endfor %}
</tbody>
</table>
</div>
% endfor %}
</div>
4.help
4.1.How to get the article_count value
4.2.I used a lot of for loop, how can reduce the for cycle
You can get article_count value by:
blog_info.article_count
Change your category_all structure for reduce loops!