How to improve this Enlive template? - clojure

I use this Enlive template to transform the HTML below it to the HTML below that. Based on a collection of twitter names I generate a table with links. How can I get rid of the Hiccup inside the enlive/clone-for?
(enlive/deftemplate usernames-table-body
"public/whoisnotfollowingme.html"
[usernames]
[:table.names :tbody :tr]
(enlive/clone-for [username usernames]
[:td]
(enlive/html-content
(html [:a {:href (str "https://twitter.com/intent/user?screen_name=" username)} username]))))
HTML input
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/bootstrap/css/bootstrap.css"/>
</head>
<body>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
<div class="container">
<div class="hero-unit">
<p class="names">These people who you are following are not following you back!</p>
</div>
<table class="names table table-striped">
<thead>
<tr>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
HTML output
<html>
<head>
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<script src="//platform.twitter.com/widgets.js" type="text/javascript"></script>
<div class="container">
<div class="hero-unit">
<p class="names">These people who you are following are not following you back!</p>
</div>
<table class="names table table-striped">
<thead>
<tr>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td> < a href =" https://twitter.com/intent/user?screen_name=foo " > foo </ a > </td>
</tr> <tr>
<td> < a href =" https://twitter.com/intent/user?screen_name=bar " > bar </ a > </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

You could change your tbody.tr template from:
<tr>
<td>name</td>
</tr>
to:
<tr>
<td>foo</td>
</tr>
Now your HTML resource is a working example of the output you want.
Then modify your deftemplate to support it:
(enlive/deftemplate usernames-table-body
"public/whoisnotfollowingme.html"
[usernames]
[:table.names :tbody :tr]
(enlive/clone-for [username usernames]
[:td :a]
(enlive/do->
(enlive/set-attr :href (str "https://twitter.com/intent/user?screen_name=" username))
(enlive/content username))))
Edited: If you want to get rid of the URL in the code, try change your href to ?screen_name= and then modify the code to something like:
(enlive/do->
(fn [node] (update-in node [:attrs :href] #(str % username)))
(enlive/content username))))
You could also make a function of it. See e.g. Append to an attribute in Enlive.

Related

tr:nth-child(even) not working with xhtml2pdf

I'm trying to add a background color to even rows in a table in HTML.
I'm using xhtml2pdf to convert the html to pdf to serve with a django backend.
The styles on the even rows don't seem to be working
<html>
<head>
<style type="text/css">
tr:nth-child(even) {
background-color: blue;
}
</style>
</head>
<body>
<main>
<table>
<thead>
<tr>
<th>Heading/th>
<th>Heading</th>
<th>Heading</th>
</tr>
</thead>
<tbody>
{% for result in data.results %}
<tr>
<td>{{result}}</td>
<td>{{result}}</td>
<td>{{result}}</td>
</tr>
{% endfor %}
</table>
</main>
</body>
</html>

Django renders template blocks in wrong order

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.)

Django admin table functionality in templates

Django has a nice sortable table system in the admin. I would like to know how can I use that in my regular templates.
I couldn´t find any info about this. Any clues welcome.
Thanks in advance.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js "> </script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$('#example').DataTable();
} );
</script>
</head>
<body>
<table id="example" class="display" >
<thead>
<tr>
<th>name</th>
<th>position</th>
<th>office</th>
<th>age</th>
<th> date </th>
</tr>
</thead>
<tbody>
{% for i in qs %}
<tr>
<td>{{ i.name }}</td>
<td>{{ i.position }}</td>
<td>{{ i.office }}</td>
<td>{{ i.age }}</td>
<td> {{ i.date }} </td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

How to send an email using email_user containing HTML and CSS in Python Django?

I am sending an activation link to the user on their registered email for my blog. I am using the following lines of code for sending the email in my signup view. I am not using EmailMultiAlternatives or send_email because, I am creating a unique token for the user signing up and I want to use email_user, which will automatically take care of for and to email address:-
current_site = get_current_site(request)
subject = 'Activate Your Account'
message = render_to_string('accounts/account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
message.content_subtype = "html"
user.email_user(subject, message)
I have added html and css in my template account_activation_email.html as under:-
{% autoescape off %}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Activate your SaralGyaan account</title>
<style type="text/css" rel="stylesheet" media="all">
[styles go there not shown due to beverity]
</style>
</head>
<body>
<span class="preheader">Use this link to activate your SaralGyaan account. The link is only valid for 24 hours.</span>
<table class="email-wrapper" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<table class="email-content" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="email-masthead">
<a href="https://saralgyaan.com" class="email-masthead_name">
SaralGyaan
</a>
</td>
</tr>
<!-- Email Body -->
<tr>
<td class="email-body" width="100%" cellpadding="0" cellspacing="0">
<table class="email-body_inner" align="center" width="570" cellpadding="0" cellspacing="0">
<!-- Body content -->
<tr>
<td class="content-cell">
<h1>Hi {{user.get_full_name}},</h1>
<p>Thank you for registering your SaralGyaan's account. Please click the link below to activate your account. <strong>This link is only valid for the next 24 hours.</strong></p>
<!-- Action -->
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<!-- Border based button
https://litmus.com/blog/a-guide-to-bulletproof-buttons-in-email-design -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
Activate your account
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p> If you have not created an account, please ignore this email or contact support if you have questions.</p>
<p>Thanks,
<br>The SaralGyaan Team</p>
<!-- Sub copy -->
<table class="body-sub">
<tr>
<td>
<p class="sub">If you’re having trouble with the button above, copy and paste the URL below into your web browser.</p>
<p class="sub">{{ protocol }}://{{ domain }}{% url 'activate' uidb64=uid token=token %}</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="email-footer" align="center" width="570" cellpadding="0" cellspacing="0">
<tr>
<td class="content-cell" align="center">
<p class="sub align-center">© 2018 SaralGyaan. All rights reserved.</p>
<p class="sub align-center">
[SaralGyaan]
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
But the email which I am receiving is not the html version of it but plain text. There is no error in this html code as I am using the same with my password reset email and it is working fine there.
You can't send HTML alone in an email. HTML is sent as an alternative to plain text.
Use the html_message to send the html part and use message to send in plain text.
message = render_to_string('template.txt', ...)
html_message = render_to_string('textmplate.html', ...)

PHP insert won't work

I have been battling with phpMyAdmin insert for sometime now,
It doesn't give me an error, yet it won't insert into the database.
My code goes like this
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="43%" id="AutoNumber1">
<tr>
<td width="100%">
<p align="left"> </td>
</tr>
<tr>
<td width="100%">
<form method="GET" name="testform" id="testform" action="logout.php">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="69%" id="AutoNumber2" height="63">
<tr>
<td width="36%" height="22">Username</td>
<td width="64%" height="22"><input type="text" name="user" size="20"></td>
</tr>
<tr>
<td width="36%" height="22">password</td>
<td width="64%" height="22">
<input type="password" name="passwd" size="20"></td>
</tr>
<tr>
<td width="36%" height="19"> </td>
<td width="64%" height="19">
<input type="submit" value="login" name="login"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Now the php also goes like this
<?php
require_once('inc/config.php');
$user = $_GET['user'];
$pass = $_GET['passwd'];
//database connection
$conn = mysqli_connect(DBHOST,DBUSER,DBPASS,DB);
//mysql anti injection
$username = mysql_real_escape_string($_GET['user']);
$password = mysql_real_escape_string($_GET['passwd']);
$sql = "INSERT INTO people (`username`, `password`) VALUES ('$username', '$password');";
mysqli_close($conn);
echo "Inserted";
?>
Trouble is, I don't see what I insert into the mysql database, when viewed on local host; what is the problem?
Seems like the issue might be the use of ';' twice at the end of the following line:
$sql = "INSERT INTO people (username, password) VALUES ('$username', '$password');";
Try the following.
$sql = "INSERT INTO people (username, password) VALUES ('$username', '$password');"