How to render the DateField on a page in django - python

Hello I just started learning django and I am having problems rending the content the way I want to. Ideally I want to manage a system of tickets and have the ticket number, start-date, end-date and status appear on the page. Similar to the below format
{% extends 'base.html' %}
{% block content %}
<h1>test</h1>
{% for cr in crs %}
<p>{{ cr }}</p>
{% endfor %}
{% endblock content %}
From what I have in my views file the only thing being displayed when i view it in my web browser is only the ticket number.
Any help would be appreciated.

You should render the attributes .start_date and .end_date in your template as well, so:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date }} {{ cr.end_date }} {{ cr.get_status_display }}
{% endfor %}
You can specify the date format with the |date template filter [Django-doc]:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date|date:"Y-m-d" }} {{ cr.end_date|date:"Y-m-d" }} {{ cr.get_status_display }}
{% endfor %}

Related

how to import a block of a template in jinja2

I want to use jinja2 to design a template that will be rendered as an article whose chapters and sections should be combined freely. So I create a base.html as a skeleton. The problem is that the chapters and sections contain some reusable table and image elements, and if I use these elements as 'macro', then it will be difficult to use chapters and sections as 'macro' too. But if I use chapters and sections as 'block', how can I import 'block' into the skeleton? And some local variables must be passed into chapters and sections.
Here is an example of my script. The script reads config.json and turns it into a dict to pass to the base.html for rendering. The actual number of chapters and sections are much more than that I represent in the example, with multiple paragraphs, and the number of type of components too.
#config.json
{"templates":[
{"title":"chapter1",
"children":[
{"title":"chapter1_1","template":"template1.html","block":"block1"},
{"title":"chapter1_2","template":"template2.html","block":"block1","parameter":{"note":"hello world"}},
]},
{"title":"chapter2","template":"template1.html","block":"block2","parameter":{"note":"blabla"}}
]}
#base.html
{% from "component.html" import common_table as common_table with context %}
{% from "component.html" import common_img as common_img with context %}
{%- for chapter in chapter_list %}
{%- set ch_loop = loop %}
<h2>{{ ch_loop.index }} {{ chapter.title }}</h2>
{%- if chapter.children %}
{%- for section in chapter.children %}
<h3>{{ ch_loop.index }}.{{ loop.index }} {{ section.title }}</h3>
{% import section.template as template with context %}
{% set parameter=section.parameter %}
{{ template.blocks[section.block] }}
{%- endfor %}
{%- else %}
{% import chapter.template as template with context %}
{% set parameter=chapter.parameter %}
{{ template.blocks[chapter.block] }}
{%- endif %}
{%- endfor %}
#template1.html
{% block block1 %}
I am block 1
{{ common_table(id="table1",head="nohead") }}
{{ common_img(src="css/icon/icon1.png") }}
{% endblock %}
{% block block2 %}
I am block 2
{{ common_img(src="css/icon/icon2.png") }}
{% if parameter.note=="blabla" %}
{{ "blabla~blabla~" }}
{% endif %}
{% endblock %}
#component.html
{% macro common_table(id, name,note, head="onehead") %}
{% if name %}
<b class="tablename">{{ name }}</b>
{% endif %}
<table id="{{ id }}" class=" {{ head }}"></table>
{% if note %}
<p class="tablenote">{{ note }}</p>
{% endif %}
{% endmacro %}
{% macro common_img(src, alt, name) %}
<img src="{{ src }}" alt="{{ alt }}">
{% if name %}
<b class="imgname">{{ name }}</b>
{% endif %}
{% endmacro %}
I searched and found that the template object in script seems to have 'blocks' attribute, so I tried to import template files as an object and use '.blocks' to get the block attribute of template object, but it gave me an error as following:
jinja2.exceptions.UndefinedError: 'jinja2.environment.TemplateModule object' has no attribute 'blocks'
I checked the code of jinja2 and found that the imported template object is 'TemplateModule' class, different from the 'Template' class in script, and the 'TemplateModule' class doesn't seem to have 'blocks' attribute, even doesn't export blocks .
So how can I import a block of a template ? Or should I change my jinja2 statements?

How to determine wagtail Page content type within django template?

The core problem is that handling of wagtail RichTextField and StreamField is radically different in the templates.
I'm trying to accomplish something similar to the following:
{% with post=post.specific %}
{% if post.content_type == 'streamfield' %}
{% include_block post.body %}
{% else %}
{{ post.body|richtext }}
{% endif %}
{% endwith %}

Loop through a Python list in Django template with for loop

I need to simplify this Django template,
{{ var.1 }}
{{ var.2 }}
{{ var.3 }}
{{ var.4 }}
{{ var.5 }}
var is Python list passed as context to the template
how do you convert the above template using a for tag construct. I tried this but does not work.
{% for i in var|length %}
{{ var.i }}
{% endfor %}
You can just do
{% for x in var %}
{{x}}
{% endfor %}

Jinja loop.index does not print

When running the following jinja code, I only get "Column info" printed. Why the index does not appears ?
{% for field in columns_form %}
{% if 'title_' in field.name %}
<td>Column {{ loop.index }} info</td>
{% endif %}
{% endfor %}
It sounds like the template is being treated as a Django template, not a Jinja template.
Using {{ loop.index }} should work in a Jinja template, but wouldn't work in a Django template, where you would use {{ forloop.counter }} instead.
In case {{ loop.index }} does not work in the latest version, a workaround is to zip the columns_form and range(0, len(columns_form)+1) in the python file as
columns_form_idx = zip(columns_form, range(0, len(columns_form)+1))
In the template file,
{% for field, idx in columns_form_idx %}
{% if 'title_' in field.name %}
<td>Column {{ idx }} info</td>
{% endif %}
{% endfor %}

django "load comment"s not working correctly

I am trying to implement a comment section on my app that keeps track of bookmarks.
This is the output that I got when I ran my local host server. It is not what I expected and I can't find out what's wrong. I expected there to be a comment form, a number for the comment count, and a comment list. But the webpage just rendered my code.
This is the code
{% extends "base.html" %}
{% load comments %}
{% block title %}Bookmark:
{{ shared_bookmark.bookmark.title|escape }}{% endblock %}
{% block head %}
<h2> Comments</h2>
{% get_comment_count for bookmarks.sharedbookmark
shared_bookmark.id as comment_count %}
{% get_comment_list for bookmarks.sharedbookmark
shared_bookmark.id as comment_list %}
{% for comment in comment_list %}
<div class="comment">
<p><b>{{ comment.user.username }}</b> said:</p>
{{ comment.comment|escape|urlizetrunc:40|linebreaks }}
</div>
{% endfor %}
<p>Number of comments: {{ comment_count }}</p>
{% comment_form for bookmarks.sharedbookmark
shared_bookmark.id %}
{% endblock %}
Thanks for your help!

Categories