I have a problem with rendering templates in Django. Blocks don't seem to appear in the DOM position they were defined in. Here is my code:
I'm using a base template (base.html):
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Partwell Transactional Email</title>
<style>
...
</style>
</head>
<body>
<span class="preheader">Partwell email</span>
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body">
<tr>
<td> </td>
<td class="container">
<div class="content">
<!-- START MAIN CONTENT AREA -->
<div class="content-main">
{% block content %} {% endblock content %}
{% block content_extended %} {% endblock content_extended %}
</div>
<!-- END MAIN CONTENT AREA -->
{% block unsubscribe %} {% endblock unsubscribe %}
<!-- START FOOTER -->
<div class="footer">
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="content-block">
{% block behalf %} {% endblock behalf %}
</td>
</tr>
<tr>
<br />
<span class="apple-link">********Amtsgericht Charlottenburg *********</span>
<span class="apple-link"> *****</span>
<br />
</tr>
</table>
</div>
<!-- END FOOTER -->
</div>
</td>
<td> </td>
</tr>
</table>
</body>
</html>
And a template that extends the base template. This template is also a base template for all transactional emails (tenant-base.html):
{% extends "base.html" %}
{% block behalf %}
This email was sent on behalf of {{ tenant.name }}.
{% endblock behalf %}
Ultimately, I'm using the tenant-base.html to create the final template:
{% extends "tenant-base.html" %}
{% block content %}
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="main">
<tr>
[...]
</tr>
</table>
{% endblock content %}
The final result looks like this:
You can see that the behalf block was rendered lower than it was originally defined. It is not the last line defined in the template, yet it moves down in the dom. I'm seeing this behavior with other blocks too. I would love to know why this happens and how to avoid it.
This is not a Django template problem but happens because one of your <tr>...</tr> has no <td>...</td> inside.
If I change your footer to this it works:
<div class="footer">
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="content-block">
{% block behalf %} {% endblock behalf %}
</td>
</tr>
<tr>
<td>
<br />
<span class="apple-link">********Amtsgericht Charlottenburg *********</span>
<span class="apple-link"> *****</span>
<br />
</td>
</tr>
</table>
</div>
(Please note the added <td>...</td> in the second table row.)
Related
here i send my details
how can i stop this for example if i run http://127.0.0.1:7000/search_acctable/?txt=Kalpesh but if now i again run my code this is run like http://127.0.0.1:7000/search_acctable/?txt=Kalpesh/search_acctable/?txt=any in django how can i solve this
i need help to solve this problem
views.py
def s_index(request):
current_url = request.build_absolute_uri()
#print(current_url)
src = request.POST.get('txt_search')
#if request.POST['btn_clear']:
# return HttpResponseRedirect(request.META.get('HTTP_REFERER')) # return to previous page
if request.POST['btn_search']:
rec=accmaster.objects.filter(Q(acc_name__contains=src) | Q(acc_city__contains=src)| Q(acc_op__contains=src) ).values() # for filter with and conition onyl put comma if want or condition use pipe sign and Q
if rec.exists():
rec=accmaster.objects.filter(Q(acc_name__contains=src)| Q(acc_city__contains=src)| Q(acc_op__contains=src)).values()
grp_city=accmaster.objects.filter( Q(acc_name__contains=src) | Q(acc_city__contains=src)| Q(acc_op__contains=src)).values('acc_city').annotate(Sum('acc_op')).order_by('acc_city')
template=loader.get_template('index.html')
output=accmaster.objects.filter(Q(acc_name__contains=src)| Q(acc_city__contains=src)| Q(acc_op__contains=src)).values().aggregate(Sum('acc_op'))
context ={
'rec':rec,
'output':output['acc_op__sum'],
'grp_city':grp_city,
}
return HttpResponse(template.render(context,request))
else :
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) # return to previous page
urls.py
from django.urls import path
from . import views
urlpatterns=[
path('',views.index,name='index'),
path('addacc/',views.add,name='addacc'),
path('addacc/addrecord/',views.addrecord,name='addrecord') ,
path('delete/<int:id>',views.delete,name='delete') ,
path('update/<int:id>',views.update,name='update'),
path('update/updaterecord/<int:id>',views.updaterecord,name='updaterecord'),
path('index/',views.s_index,name='s_index'),
#path('',views.form_view,name='mform')
]
index.html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function printreport(){
//var divtoprint=document.getElementById("maindiv");
var printcontext=document.getElementById("maindiv").innerHTML;
var originalcontext=document.body.innerHTML;
var nwin=window.open("");
nwin.document.open();
nwin.document.write('<html><head><link rel="stylesheet" media="print" href="{% static 'mystyleprint.css' %}" ></head><body>');
nwin.document.write(printcontext);
nwin.document.write("</body></html>");
//document.write(printcontext);
//document.body.innerHTML=printcontext;
//printWindow.document.write(divtoprint);
nwin.print();
nwin.document.close();
nwin.close();
}
</script>
<link rel="stylesheet" href="{% static 'mystyle.css' %}" >
<link rel="stylesheet" href="{% static 'mystyleprint.css' %}" media="print"> <!-- make seprate css for print document and make media print-->
</head>
<body >
<form action="index/" method="post" >
{% csrf_token %}
<div>
<button type="button">Add Account</button>
<label>Search :</label> <input id="txt_search" name="txt_search" autocomplete="off">
<input type="submit" id="btn_search" name="btn_search" value="Search" onclick="myfunction()">
<input type="button" id="btn_clear" name="btn_clear" value="clear"" onclick="history.back()">
<input type="button" name="btn_print" value="Print" onclick="printreport()">
</div>
<br>
<div id="maindiv">
<table id="maintable">
{% with no="s" %}
<h1> Account List </h1>
<tr>
<th> Sr.No </th>
<th> Name </th>
<th> City </th>
<th> Opening Balance </th>
<th id="thedit"> Edit </th>
<th id="thdelete"> Delete </th>
</tr>
{% for y in grp_city %}
<tr>
<td id="tdcity" colspan=4 style="color:magenta"> {{ y.acc_city }}</td>
{% for x in rec %}
{% if x.acc_city == y.acc_city %}
<tr>
<td style="width:4%" id="srno"></td>
<td>{{ x.acc_name }}</td>
<td style="width:20%"> {{ x.acc_city}}</td>
<td align="right" style="width:10%"> {{ x.acc_op}}</td>
<td style="width:4%" id="redit"> <img src="{% static 'icon/update.png' %}"></td>
<td style="width:4%" id="rdelete"> </td>
</tr>
{% endif %}
{% endfor %}
<td colspan=4 align="right" style="color:magenta; font-size:18px;" >Total: {{ y.acc_op__sum|floatformat:2 }} </td>
<td colspan=2 id="nodisp"> </td>
</tr>
{% endfor %}
<tr>
<td colspan=4 align="right" style="color:red; font-size:20px">Total : {{output|floatformat:2}} </td>
</tr>
</table>
</div>
<p>
</p>
{% endwith %}
</form>
</body>
</html>
i don't know to how to handle it i am new to django
i consider that why template not render tag after {% include %}. when i put some tag like something in front of include tag, it work. But it not work if i try to put behind the include tag. :(
in index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'styles/main.css' %}" />
<script
src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/js/bootstrap.bundle.min.js"
integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous">
</script>
</head>
<body>
<div class="d-flex">
{% include 'navbar.html' %}
<div class="content">
<div class="header">
{% include 'header.html' %}
</div>
<div>
{% block subHeader %}
{% endblock %}
</div>
<div>
{% block list %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
in list.html
<table class="table">
<thead>
<tr>
<th scope="col">Cardinal Number</th>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Category</th>
<th scope="col">Cost</th>
<th scope="col">Note</th>
<th scope="col">Image</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
in products.html
{% extends 'index.html' %}
{% block subHeader %}
{% include 'components/subHeader.html' with url="api/add-product" name="product"
selectName="product-category" %}
{% endblock subHeader%}
{% block list %}
{% include 'components/list.html' %}
{% endblock content%}
although i put whatever after include tag in block subHeader, it not work. i dont understand why that. Can someone point me ? Thanks
I have a webapp that renders a table of content. Content to be rendered in returned from a backend and Jinja2 is used to cycle through the data to put into appropriate columns and rows. This part works exactly as I expect. A web user performs some operation with data at each row using some buttons on each row.
In case the backend needs to display messages, I use flash(), with the message(s) rendered at the end of the table. Again, as constructed this works as I expect.
However, as the number of rows of the table grow, the place where messages are flash()'d wind up scrolling off the screen. So I was wondering if I could render flash()'d messages at each row. The intent is that the user receives messages as needed directly under each "affected" row.
The main challenge is to hide the message rows until a message needs to be rendered, and I am not 100% sure how best to do that.
An alternative I was considering is to not use flash(), but deliver additional content as needed for "message" rows. I guess I still have the challenge of checking if anything needs to be rendered at a particular row, and how best to do that...
Here's my current HTML:
<table class="styled-table">
<thead>
<tr>
<th>
Filename
</th>
<th width="70%">
Content
</th>
<th>
id
</th>
<th></th> <!-- edit file button -->
<th></th> <!-- delete file button -->
</tr>
</thead>
<tbody>
{% for content in filesContent %}
<tr>
<td>
<!-- file name -->
<div>
{{ content[2] }}
</div>
</td>
<td>
<!-- file content -->
{{ content[3] }}
</div>
</td>
<td>
<div>
{{ content[1] }}
</div>
</td>
<td>
<td>
<!-- edit button -->
<div>
<a id="{{ content[2] }}" href="/editFile/{{ content[2] }}"><img
src="{{ url_for('static', filename='images/edit_small.png') }}"
alt="edit"></a>
</div>
</td>
<td>
<!-- delete button -->
<div>
<a id="{{ content[2] }}" href="/deleteFile/{{ content[2] }}"><img
src="{{ url_for('static', filename='images/trash_small.png') }}"
alt="delete"></a>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
<!--
The backend script returns messages using flash().
-->
<div class="fromBackEnd">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for message in messages %}
{% if "Error" not in message: %}
<div class="alert alert-info">
{{ message[1] }}
</div>
{% endif %}
{% if "Error" in message: %}
<div class="alert alert-warning">
{{ message[1] }}
</div>
{% endif %}
{% endfor %}
{% endif %}
{% endwith %}
</div>
What I'd like to be to do is something like (HTML simplified for brevity):
<table>
{% for content in filesContent %}
<tr>
... my current td content ...
<tr>
<!-- NEW MESSAGE ROWS TO BE DISPLAYED ONLY IF 'messageContent' not null -->
<tr id="<SOMEUNIQUEID>" style="display:none">
<!-- span across all columns -->
<td>
{{ messageContent }}
</td>
</tr>
{% endfor %}
</table>
This way, messages are rendered right under the "affected" row, and easier for users to see. I could use some javascript to change the display style, but maybe I could just use {% if... %} around the tr??
Any guidance would be greatly appreciated!!!
I have this piece of code :
{% extends 'base.html' %}
{% load static %}
{% block page_title %}Manage Staff{% endblock page_title %}
{% block content %}
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Manage All Staff</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>SN</th>
<th>Full Name</th>
<th>Email</th>
<th>Course</th>
</tr>
</thead>
<tbody>
{% for staff in allStaff %}
<tr>
<td>-</td>
<td>{{staff.last_name}}, {{staff.first_name}}</td>
<td>{{staff.email}}</td>
<td>{{staff.course.name}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
{% endblock content %}
The block page_title is used to display the current page in base.html.
My question is, how can I avoid repeating the text "Manage Staff" ?
Is there a way to display the block page_title in this current file ?
What I earlier tried was passing the value from my views.py file, so I wanna be sure I am doing the right thing
I am working on a project right now, particularly on the CRUD phase. I am also new on the Django framework, but I knew some basic stuff about it. I did everything stated on the manual but it's weird because I got this kind of error.
I am using Django 2.2. Here's my code, by the way.
base.html
<!-- base.html -->
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>{% block title %}Title{% endblock %}: CAO-SMS Admin</title>
{% load static %}
<link rel = "stylesheet" href = "{% static 'fisher_prof/style.css' %}"/>
</head>
<body>
{% block body %}Body{% endblock %}
</body>
</html>
list.html
<!-- Displays the whole database (fisher) -->
{% extends 'fisher_prof/base.html' %}
{% block title %} Fisher's Information {% endblock %}
{% block body %}
<br>
<center>
<h1>Fisher's Database</h1>
<table class = "table table-striped table-bordered table-sm">
<thead class = "thead-dark">
<tr>
<th>ID  </th>
<th>Fisher's Last Name</th>
<th>Fisher's First Name</th>
<th>Fisher's Middle Name</th>
<th>Mobile Number</th>
<th>Actions</th>
</tr>
</thead>
{% for fisher in fishers %}
<tr>
<td>{{fisher.last_name}}</td>
<td>{{fisher.first_name}}</td>
<td>{{fisher.mid_name}}</td>
<td>{{fisher.cell_num}}</td>
</tr>
{% endfor %}
</table>
<br> <br>
</center>
{% endblock %}
Then when I run it, Django tells me this:
Invalid block tag on line 41: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Is there something wrong with my code? I really need help.