Converting html + bootstrap files (using flex) into pdf - Django? - python

I am trying to create a webapp where, the data comes from a certain API and is saved into the database. Now, I want to create a pdf using the data. I had my html Code ready with custom css and bootstrap ( using display: flex; ). I already tried using the xhtml2pdf, reportlab libraries but am not able to get it right with CSS flex properties.
The source for the HTML, and some python file are attached below.
HTML File:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 5.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Invoice-ID</title>
<style type="text/css">
/* Bootstrap CSS */
.col-md-7 {
flex: 0 0 auto;
width: 58%;
}
.container {
width: 1320px;
}
.bg-light {
background-color: #f8f9fa !important;
}
.nav {
display: flex;
flex-wrap: wrap;
padding-left: 0;
margin-bottom: 0;
list-style: none;
}
.justify-content-center {
justify-content: center;
}
.navbar {
position: relative;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
padding-top: .5rem;
padding-bottom: .5rem;
}
.navbar>.container-fluid {
display: flex;
flex-wrap: inherit;
align-items: center;
justify-content: space-between;
}
.navbar-light .navbar-brand {
color: rgba(0, 0, 0, .9);
}
.navbar-brand {
padding-top: .3125rem;
padding-bottom: .3125rem;
margin-right: 1rem;
font-size: 1.25rem;
text-decoration: none;
white-space: nowrap;
}
.flex-column {
flex-direction: column !important;
}
.d-flex {
display: flex !important;
}
.w-50 {
width: 50% !important;
}
.ms-2 {
margin-left: .5rem !important;
}
.p-3 {
padding: 1rem !important;
}
/* Custom CSS */
body {
font-weight: 300;
font-size: 14px;
}
.wrapper {
border: 2px solid gray;
border-width: 2px;
}
#location {
/* border: 2px solid gray; */
border-right: 1px solid gray;
border-width: 1px;
margin-bottom: 2rem;
}
.header {
font-size: 20px;
font-weight: 100;
text-align: center;
color: #007cae;
}
.title {
font-size: 22px;
font-weight: 100;
padding: 10px 20px 0px 20px;
}
.title span {
color: #007cae;
}
.details {
padding: 10px 20px 0px 20px;
text-align: left !important;
}
/* . {
margin-left: 455px;
} */
.hritem {
border: 1px solid gray;
border-width: 2px;
}
</style>
</head>
<body>
<div class="mt-5 container" style="border: 2px solid gray;">
<!-- Header Start -->
<div class="d-flex justify-content-center align-items-center">
<!-- <img src="E:\Python\Django web\logiswift\Deployment\main\static\assets\logo.png" alt="Logo" width="350px"> -->
<h1>LOGO</h1>
<div class="ms-auto">
<span><b>Some Text</b></span>
</div>
</div>
<hr class="hritem"/>
<div class="d-flex justify-content-center align-items-center">
<h3>Title</h3>
<div class="ms-auto ">
<p>Date: 12-12-2021</p>
<p>Document ID: 9868</p>
</div>
</div>
<!-- Header End -->
<hr class="hritem"/>
<!-- General Trip Info Start -->
<div class="d-flex">
<div class="w-50" id="location">
<p>
<h6>Current Location</h6>
<span>Address Line 1,</span>
<span>Address Line 2,</span>
<span>City,</span>
<span>State - PIN</span>
</p>
<p>
<h6>Expected Location</h6>
<span>Address Line 1,</span>
<span>Address Line 2,</span>
<span>City,</span>
<span>State - PIN</span>
</p>
</div>
<div class="w-50 ms-2 mt-3" id="consumer">
<h6>consumer Details</h6>
<hr/>
<p>
<span style="font-size: medium; font-weight: 400;"><b>Name: </b></span>
<span>Your Name</span>
</p>
<p>
<span style="font-size: medium; font-weight: 400;"><b>Consumer ID: </b></span>
<span>Unique Consumer ID</span>
</p>
</div>
</div>
<!-- General Trip Info End -->
<hr class="hritem"/>
<!-- Bill Start -->
<!-- <div>
<p>Bill Will Come here</p>
</div> -->
<!-- Bill End -->
<!-- <hr class="hritem"/> -->
<!-- Footer Start -->
<!-- <div>
<p>Additional info like address of the company and seal comes here.</p>
</div> -->
<!-- Footer end -->
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
utils.py
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
view.py
def generatePDF(request):
template = get_template('main/invoices.html')
context = {
"date": datetime.date.today(),
}
html = template.render(context)
pdf = render_to_pdf('main/invoices.html', context)
return HttpResponse(pdf, content_type="application/pdf")
I don't understand why the above code isn't rendering the CSS flex attributes. I even tried to use the bootstrap classes inside the style tag, but the CSS flex won't come on rendering the PDF.
Please help me out.
Thanks in advance

Related

Not able to get transparent dropdown menu

I am building a website using flask-python, css, HTML.
I am not able to get transparent dropdown menu i changed the styling for css code and given proper code to HTML Layout.
The dropdown occurs in the center without any styling which i had speified in CSS styling
If i write the code in only in HTMl and CSS for dropdown menu.The drop down menu works perfectly fine.
Main python Script
from flask import Flask,render_template
import os
from flask import send_from_directory
app = Flask(__name__)
#app.route('/')
def home():
return render_template("home.html")
#app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
#app.route('/projects/')
def projects():
return render_template("projects.html")
if __name__ == "__main__":
app.run(debug = True)
layout code layout.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<body>
<head>
<meta charset="utf-8">
<title>Developer | Krishan</title>
<link rel = "stylesheet" href ="{{ url_for('static' , filename = 'css/main.css') }}">
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
</head>
<header>
<div class="container">
<h1 class = "logo">Krishan K B's Webpage</h1>
<strong><nav>
<ul class = "menu">
<li>Home</li>
<li>Projects
<ul class = "menu">
<li><a>Disaster Managment</a></li>
<li><a>Data Analytics</a></li>
<li><a>Volcanoes & Population</a></li>
<li><a>Games</a></li>
<li><a>Book App</a></li>
</ul>
</li>
</ul>
</nav></strong>
</div>
</header>
<div class = "container">
{%block content%}
{%endblock%}
</div>
</body>
</html>
Style css code style.css
body{
margin: 0 auto;
text-align: center;
font-family: brandon-grotesque,HelveticaNeue-Light,"Helvetica Neue Light","Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
}
ul {
margin: 0px;
padding: 0px;
list-style : none;
}
ul li {
float : left;
width : 200px;
height : 40px;
background-color: #020202;
opacity : .8;
line-height: 40px;
text-align: center;
font-size: 20px;
margin-right: 2px;
}
ul li a {
text-decoration: none;
color : white;
display: block;
}
ul li a:hover {
background-color: green;
}
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
h1{
text-align: center;
font-family: 'Yellowtail', cursive;
color: #e46922;
margin: 50px auto 0 auto;
font-size:60px;
}
h2{
color:#66bfbf;
text-align: center;
font-family:'monntserrat',sans-serif;
padding-bottom: 10px;
}
h3{
color: #11999e;
font-family:'monntserrat',sans-serif;
}
p{
line-height: 2;
width: 40%;
margin: 40px auto 60px;
}
hr{
border: dotted #eaf6f6 6px;
border-bottom: none;
width: 6%;
margin: 100px auto;
}
a{
color: #80b838;
font-family:'monntserrat',sans-serif;
margin: 10px 20px;
text-decoration: none;
}
a:hover{
color: #eaf6f6;
}
.head{
color: #66bfbf;
font-size: 140%;
}
.top{
background-color: #E4F9F5;
position: relative;
padding-top: 100px;
}
.middle{
margin: 100px 0;
}
.bottom{
background-color: #66bfbf; ;
padding: 50px 0 10px;
}
.intro{
width: 30%;
margin: auto;
}
.top-cloud{
position: absolute;
right: 180px;
top: 30px;
}
.below-cloud{
position: absolute;
left: 150px;
bottom: 230px;
}
.img{
float: left;
margin-left: 320px;
}
.img-code{
float: right;
margin-right: 320px;
}
.skils{
width: 30%;
margin: auto;
}
.copyright{
color: #eaf6f6;
font-size: 0.75rem;
}
.contact-message{
width: 40%;
margin: 40px auto 60px;
}
.contact{
padding-bottom: 100px;
}
.btn {
margin: 100px;
background: #45b29a;
background-image: -webkit-linear-gradient(top, #45b29a, #45b29a);
background-image: -moz-linear-gradient(top, #45b29a, #45b29a);
background-image: -ms-linear-gradient(top, #45b29a, #45b29a);
background-image: -o-linear-gradient(top, #45b29a, #45b29a);
background-image: linear-gradient(to bottom, #45b29a, #45b29a);
-webkit-border-radius: 6;
-moz-border-radius: 6;
border-radius: 6px;
font-family: Arial;
color: #1b7f69;
font-size: 21px;
padding: 10px 20px 10px 20px;
text-decoration: none;
}
.btn:hover {
background: #45b29a;
background-image: -webkit-linear-gradient(top, #45b29a, #1b7f69);
background-image: -moz-linear-gradient(top, #45b29a, #1b7f69);
background-image: -ms-linear-gradient(top, #45b29a, #1b7f69);
background-image: -o-linear-gradient(top, #45b29a, #1b7f69);
background-image: linear-gradient(to bottom, #45b29a, #1b7f69);
text-decoration: none;
}
Output i received :
enter image description here
I think your css is not properly rendered by flask, you need to hard reload
After this go to developers tool > sources tab , then search for the css file and crosscheck whether it is properly rendered or not.
Thanx i did hard reload and it worked but now i am not able to see the dropdown or any menu in my home page but i can see it in my projects webpage when i type /projects/ with the web address local host on URL. All the css styling changes are working perfectly fine but now there is no options menu in in my HomePage.
This is my homepage with no dropdown or any menu options to go to projects webpage :
enter image description here
This is my projects webpage, in this there is a dropdown and a menu with all the styling.
enter image description here
Homepage Code:
{%extends "layout.html"%}
{%block content%}
<div class = "home">
<div class="top">
<img class="top-cloud" src="{{ url_for('static' , filename = 'images/clo.png') }}" alt="cloud-img" height="200px">
<h1> I'm Krishan.</h1>
<p class="head">A Back End Developer.</p>
<img class="below-cloud" src="{{ url_for('static' , filename = 'images/clo.png') }}" alt="cloud-img" height="200px">
<img src="{{ url_for('static' , filename = 'images/land.svg') }}" alt="mounntains-img" height="350px">
</div>
<div class="middle">
<img class="profile" src="{{ url_for('static' , filename = 'images/profile.png') }}" alt="profile" height="150px" >
<h2>Hi.</h2>
<P class="intro"> I'm a Programme developer,web developer and designer,UI Designer based in Bangalore,India.I have a passion for web app,data analytics,web design,data analytics and coding and love to create new apps,website for web and mobile devices.</P>
</div>
<hr>
<div>
<h2>What I can do.</h2>
<img class="img" src="{{ url_for('static' , filename = 'images/coding.png') }}" alt="" height="150px" >
<p>I like to keep it simple. My goals is to develop a new programme which would be beneficial for the people and be enviromen friendly.</p>
</div>
<hr>
<div class="skill">
<h2>Develop what you need.</h2>
<img class="img-code" src="{{ url_for('static' , filename = 'images/code-img.png') }}" alt="" height="150px" >
<p class="skills">I'm a developer, so I know how to create your website,application or any other programme to run across devices using the latest technologies available.</p>
</div>
<hr>
<div class="contact">
<h2>Get in touch.</h2>
<h3>I’m currently available for freelance work.</h3>
<p class="contact-message">if you have a project that you want to get started, think you need my help with something or just fancy saying hey, then get in touch.</p>
<a class="btn" href="mailto:krishankbhushan#gmail.com">message me</a>
</div>
</div>
<div class="bottom">
<a class="footer-link" href="https://www.linkedin.com/in/krishan-k-b-0a164b125/">Linkedin</a><i class="fab fa-twitter"></i>
<a class="fab fa-twitter" href=" https://twitter.com/krishankbhushan"><i class="fa fa-twitter"></i>Twitter</a>
<i class="fa fa-twitter" style="font-size:24px"></i>
<p class="copyright">©️ 2021 Krishan.All rights reserved.</p>
</div>
</div>
{%endblock%}

How can I get the image to show in the img container, using Django template tags?

So I am at the last couple details of my portfolio website, and everything has gone relatively smoothly. I have not had an issue with images before, but this time I am using a {% for loop %} to iterate over the code for the card and I just have the variable template tags that create a new object every time I add something new in the django admin interface.
I feel like there is some small detail I am just completely missing, because I had it working at one point, but then decided to do the iteration instead of hard-coding it in, and it just won't show up. Another pair of eyes would be nice.
This is the section of my index.html:
{% block content %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Jordan Miracle</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/static/portfolio/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<body>
<div class="jumbotron text-center ui-datepicker-next-hover" style="margin-bottom:0">
<div class="align-center">
<img src="{% static 'portfolio/mylogo.png' %}" id="img-bastet" class="img-fluid"
alt="Jordan and Bastet">
</div>
<h1><p style="font-family: 'Ubuntu Mono', Monospaced, monospace; font-size: medium;">Developer and Aspirant Data
Scientist</p>
</div>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="{% url 'home' %}">Home</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="https://github.com/jordanmiracle">Github</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://twitter.com/jordanmiracle1">Twitter</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://www.linkedin.com/in/jordan-m-605b4b1a9/">LinkedIn</a>
</div>
<div class="topnav-right">
Blog
</nav>
<div class="container" style="margin-top:30px">
<div class="row">
<h2>About Me</h2>
<div class="row justify-content-center my-3">
<div class="col-5" style="border: #303030" aria-setsize="10px">
<img src="{% static 'portfolio/me-bastet-and-babys-turtle.jpg' %}" class="img-fluid"
alt="Jordan and Bastet">
</div>
</div>
<div class="intro-text">
<p style="font-family: 'Ubuntu Mono', Monospaced, monospace; font-size: small; ">
<p>I enjoy web development, and have a true love for analyzing data. Whether creating visualizations and
interpreting what I find,<br>
or creating a machine learning model to try and make predictions. <br>
Things that interest me the most are the climate and our ecosystem, consciousness, and the cosmos that we live
in.
</p>
</div>
<ul class="nav nav-pills flex-column">
<div class="button-row-main">
<button class="nav-button highlight-green">
<a href="mailto:jordanmiracle#protonmail.com" target="_blank">Contact
Me</a>
</button>
</div>
<div class="portfolio">
{% for project in projects %}
<div class="project">
<h2>{{ project.title }}</h2>
<p class="technologies">
</p>
<!-- Depending on availability, display either the image as a link,
only image, or a string explaining that the img is missing-->
{% if project.image %}
<a href="{{ project.url }}" target="_blank">
<div class="img-container" style="background-image: url('{{ project.image }}')"></div>
</a>
<p>{{ project.description }}
</p><br>
<div class="project-links">
Live Site
Code
</div>
{% endif %}
</div>
{% endfor %}
</body>
{% endblock %}
</html>
My style.css:
font-family: 'Oswald', sans-serif;
text-align: center;
background-color: #00FF00;
color: #303030;
}
/* ---------- HOME ---------- */
/* create color-hover-effect for M logo and buttons */
.highlight-orange:hover {
color: #417FD5;
border-color: #ffa000;
}
.highlight-green:hover {
color: darkred;
border-color: indianred;
}
button a {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
text-decoration: darkslateblue;
color: inherit;
/* increase the clickable area */
display: inline-block;
position: relative;
z-index: 1;
padding: 1.3em;
margin: -1.3em;
}
.nav-button {
font-size: 1.1em;
margin: 20px;
padding: 10px;
border: 2px solid #303030;
border-radius: 25px;
color: #303030;
background-color: #fdfdfd;
box-shadow: #417FD5;
}
.button-row-mobile,
.button-row-portfolio-mobile {
display: none;
}
/* ---------- PORTFOLIO ---------- */
.portfolio {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.project {
margin: 20px;
padding: 20px;
border: 3px solid #303030;
border-radius: 20px;
color: transparent;
background-color: transparent;
max-width: 300px;
}
.project-links {
display: flex;
justify-content: space-around;
}
.project-links a {
color: #4DAAE3;
}
.project-links a:visited {
color: #ffffff;
}
.project-links a:hover {
color: #4DAAE3;
}
.button-row-portfolio {
position: fixed;
width: 100%;
justify-content: space-between;
bottom: 5px;
}
.button-row-portfolio .nav-button {
opacity: 0.8;
}
/*
restrict img to uniform size automatically,
while showing the center portion of the screenshot. adapted from:
https://stackoverflow.com/questions/15866223/how-to-make-images-get-cut-off
*/
.img-container {
height: 200px;
width: 250px;
color: transparent;
background-position: center;
border: 1px solid #303030;
border-radius: 20px;
background-repeat: no-repeat;
}
/* ---------- FOOTER ---------- */
.separator {
display: none;
}
#footer {
position : absolute;
bottom : 0;
height : 40px;
margin-top : 40px;
}
footer {
font-size: 16px;
color: #303030;
}
.site-footer {
padding-left: 40px;
padding-right: 40px;
padding-top: 15px;
padding-bottom: 5px;
background-color: #aa2222;
color: white;
font-style: italic;
text-align: center;
}
.icon {
height: 36px;
width: auto;
padding: 8px;
}
.overflow {
color: #fdfdfd;
font-size: 2.5em;
}
/* ---------- RESPONSIVE ELEMENTS ---------- */
/* TODO: remove blue highlighting when buttons are clicked */
#media only screen and (max-width: 768px) {
.logo {
font-size: 14em;
}
.button-row-main,
.button-row-portfolio {
display: none;
}
.button-row-mobile {
margin-top: 50px;
margin-bottom: 50px;
display: flex;
justify-content: space-around;
}
.button-row-portfolio-mobile {
display: flex;
position: fixed;
width: 100%;
justify-content: space-around;
bottom: 5px;
}
.button-row-portfolio-mobile .nav-mobile {
opacity: 0.8;
}
.nav-mobile {
font-family: 'Oswald', sans-serif;
font-size: 1em;
border: none;
border-radius: 100px;
background-color: #fdfdfd;
}
.highlight-orange {
color: #4DAAE3;
border-color: #4DAAE3;
fill: #4DAAE3;
}
.highlight-green {
color: lightgreen;
border-color: lightgreen;
fill: lightgreen;
}
.highlight-blue {
color: lightblue;
border-color: lightblue;
fill: lightblue;
}
.separator {
display: block;
}
footer {
margin-top: 24px;
}
.icon {
height: 45px;
width: auto;
padding: 20px;
}
}
* {
box-sizing: border-box;
}
body {
background-color: #f1f1f1;
padding: 20px;
font-family: "Ubuntu Mono", serif;
}
/* Center website */
.main {
max-width: 1000px;
margin: auto;
}
h1 {
font-size: 50px;
word-break: break-all;
}
.row {
margin: 8px -16px;
}
/* Add padding BETWEEN each column (if you want) */
.row,
.row > .column {
padding: 8px;
}
/* Create four equal columns that floats next to each other */
.column {
float: left;
width: 25%;
}
/* Clear floats after rows */
.row:after {
content: "";
display: table;
clear: both;
}
/* Content */
.content {
background-color: white;
padding: 10px;
}
/* Responsive layout - makes a two column-layout instead of four columns */
#media screen and (max-width: 900px) {
.column {
width: 75%;
}
}
/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
#media screen and (max-width: 600px) {
.column {
width: 100%;
}
}
My models.py:
class Project(models.Model):
title = models.CharField(max_length=100)
description = models.CharField(max_length=120)
image = models.FilePathField(path='portfolio/images')
url = models.URLField(blank=True)
code_url = models.URLField(blank=True)
def __str__(self):
return self.title
Here is my file structure.
If anybody needs more information, or if I need to provide more details, just let me know. This is the last thing really standing in my way of deploying and it's been a pain.
Before changing it to img class
After changing it from a div class to img class
The images needed to be registered properly in the Django admin shell.
Once in the shell, I had to import the models.
from portfolio.models import Project
Then, p2 = Project.objects.get(id=2)
p2.image = <path_to_file>
And so on. Once they were registered in the DB, they popped up right away.
Looks like you might need to set a background image size on .img-container. If that doesn't work, also try setting display: block;
.img-container {
height: 200px;
width: 250px;
color: transparent;
background-position: center;
border: 1px solid #303030;
border-radius: 20px;
background-repeat: no-repeat;
background-size: cover;
display: block;
}

Fetch "Some" data from MySQL Database using Flask (Python)

I have just started making a "Student Database Project" with Flask. I have set up a MySQL Database using the flask_mysqldb module. I have also inserted some student data into it. But I want to fetch the data (name, grade, phone number, address, email , etc) of a specific student. How should I do it? Please help me because I'm new to Flask and MySQL. Here's my code:
from flask import Flask,render_template, redirect, url_for, request, jsonify
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['SECRET_KEY'] = 'ILUVTOFART'
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'student_db'
mysql = MySQL(app)
#app.route("/add_s_data", methods=["GET","POST"]) #This is the page for adding student information to DB
def handle_s_data():
if request.method == "POST":
cur = mysql.connection.cursor()
sid = request.form.get('id', False) # Student ID
name = request.form.get('name', False) # Full Name
f_name = request.form.get('fname', False) # Father's Name
m_name = request.form.get('mname', False) # Mother's Name
clas = request.form.get('class', False) # His grade/class
roll = request.form.get('roll', False) # His roll number
section = request.form.get('section', False) # His class section
dob = request.form.get('dob', False) # Date of Birth
join = request.form.get('join', False) # Admission date
phone = request.form.get('phone', False) # Phone number
address = request.form.get('address', False) # Address
sequence = (sid, name, f_name, m_name, clas, roll, section, dob, join, phone, address)
formula = "INSERT INTO student_data (id, name, f_name, m_name, class, roll, section, dob, joindate, phone, address) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s )"
cur.execute(formula,sequence)
mysql.connection.commit()
return str(sequence) + " Done!"
return render_template('add_s_data.html')
#I want to fetch these data from the database and show them on the page. I know how to use Jinja2 Variables, but how do I fetch the data?
#import url('https://fonts.googleapis.com/css?family=Josefin+Sans&display=swap');
#import url('https://fonts.googleapis.com/css?family=Noto+Sans+TC&display=swap');
#import url('https://fonts.googleapis.com/css?family=Lato&display=swap');
#import url('https://fonts.googleapis.com/css?family=Raleway&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
text-decoration: none;
font-family: 'Josefin Sans', sans-serif;
}
body{
background-color: #f3f5f9;
}
.wrapper{
display: flex;
position: relative;
}
.wrapper .sidebar{
width: 240px;
height: 100%;
background: #0080ff;
padding: 30px 0px;
position: fixed;
}
.wrapper .sidebar h2{
margin-left: 15px;
padding-top: 05px;
padding-bottom: 05px;
color: white;
font-family: 'Noto Sans TC', sans-serif;
-webkit-text-stroke: 0.5px black;
}
.wrapper .sidebar ul li{
padding: 15px;
border-bottom: 1px solid #bdb8d7;
border-bottom: 1px solid rgba(0,0,0,0.05);
border-top: 1px solid rgba(255,255,255,0.05);
}
.wrapper .sidebar ul li a{
color: white;
display: block;
}
.wrapper .sidebar ul li a .fas{
width: 25px;
}
.wrapper .sidebar ul li:hover{
background-color: #0f52ba;
}
.wrapper .sidebar ul li:hover a{
color: #fff;
}
.wrapper .sidebar .social_media{
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.wrapper .sidebar .social_media a{
display: block;
width: 40px;
background: #594f8d;
height: 40px;
line-height: 45px;
text-align: center;
margin: 0 5px;
color: #bdb8d7;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.home {
background: #0080ff;
}
.add{
background: #0f52ba;
}
.wrapper .main_content{
width: 100%;
margin-left: 200px;
}
.wrapper .main_content .header{
padding: 20px;
background: #fff;
color: #717171;
border-bottom: 1px solid #e0e4e8;
}
.wrapper .main_content .info{
margin: 20px;
color: #717171;
line-height: 25px;
}
.wrapper .main_content .info div{
margin-bottom: 20px;
}
#media (max-height: 500px){
.social_media{
display: none !important;
}
}
.id{
font-size: 30px;
margin-top: 5%;
margin-left:20%;
}
.id input{
margin-left: 16.5%;
width: 30%;
height: 5;
font-size: 30px;
}
.name{
font-size: 30px;
margin-top: 3%;
margin-left:20%;
}
.name input{
width: 30%;
height: 5;
font-size: 30px;
margin-left: 11.5%;
}
.fname{
font-size: 30px;
margin-top: 3%;
margin-left:20%;
}
.fname input{
width: 30%;
height: 5;
margin-left: 1%;
font-size: 30px;
}
.mname{
font-size: 30px;
margin-top: 3%;
margin-left:20%;
}
.mname input{
width: 30%;
height: 5;
font-size: 30px;
}
.class{
font-size: 30px;
margin-top: 3%;
margin-left:20%;
}
.class input{
width: 30%;
height: 5;
font-size: 30px;
margin-left: 12.5%;
}
.roll{
font-size: 30px;
margin-top: 3%;
margin-left:20%;
}
.roll input{
width: 30%;
height: 5;
font-size: 30px;
margin-left: 14%;
}
.section{
font-size: 30px;
margin-top: 3%;
margin-left:20%;
}
.section input{
width: 30%;
height: 5;
font-size: 30px;
margin-left: 9.5%;
}
.dob{
position: absolute;
margin-left: 65%;
top: 18.5%;
font-size: 30px;
}
.dob input{
width: 50%;
font-size: 30px;
height: 1;
}
.joining{
position: absolute;
margin-left: 65%;
top: 29.5%;
font-size: 30px;
}
.joining input{
margin-left: 2%;
width: 50%;
font-size: 30px;
height: 1;
}
.phone{
position: absolute;
margin-left: 65%;
top: 42.5%;
font-size: 30px;
}
.phone input{
margin-left: 10%;
width: 50%;
font-size: 30px;
height: 1;
}
.address{
position: absolute;
margin-left: 65%;
top: 54.5%;
font-size: 30px;
}
.address input{
margin-left: 16%;
width: 51%;
font-size: 30px;
height: 1;
}
button{
font-size: 20px;
border: 1px solid black;
background: lightblue;
color: white;
margin-left: 77%;
position: absolute;
top: 70%;
width: 100px;
height: 50px;
border-radius: 8px;
}
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>I am Ahnaf</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename = 'css/bootstrap.min.css')}}">
<link rel="stylesheet" type="text/css" href="../static/css/add_s_data.css">
<link rel="stylesheet" type="text/css" href="../static/fontawsome/css/all.css">
</head>
<body>
<!-- Sidebar Section -->
<div class="wrapper">
<div class="sidebar">
<h2>Ahnaf's SDMS</h2>
<ul>
<li class="home"><i class="fas fa-home"></i>Home</li>
<li class="search"><i class="fas fa-search"></i>Search Data</li>
<li class="add"><i class="fas fa-users"></i>Add Data</li>
<li class="modify"><i class="fas fa-edit"></i>Modify Data</li>
<li class="delete"><i class="fas fa-user-minus"></i>Delete Data</li>
<li class="report"><i class="fas fa-flag"></i>Add Student Report</li>
<li class="present"><i class="fas fa-address-card"></i>ID Card Maker</li>
<li class="exam"><i class="fas fa-pen"></i>Exams</li>
<li class="events"><i class="fas fa-calendar-week"></i>Events</li>
<li class="contact"><i class="fas fa-address-book"></i>Contact</li>
</ul>
</div>
</div>
<div class="choice">
<p style="color:blue;position: absolute; top:2%;margin-left: 35%;font-size: 30px;">Student</p>
<hr style="position: relative; margin-top: 4%; margin-left: 29%; width: 20%">
<div style="position:absolute;top:0;border-left:1px solid #000;height:50px; margin-left: 57%;"></div>
<a style="color: black;position: absolute; top:2%;margin-left: 70%;font-size: 30px;"href="/add_t_data">Teacher</a>
</div>
<div class="form">
<form action="/add_s_data" method='POST'>
<div class="id">
<label>ID:</label>
<input type="text" name="id" ><br>
</div>
<div class="name">
<label>Name:</label>
<input type="text" name="name" ><br>
</div>
<div class="fname">
<label>Father's Name:</label>
<input type="text" name="fname" ><br>
</div>
<div class="mname">
<label>Mother's Name:</label>
<input type="text" name="mname" ><br>
</div>
<div class="class">
<label>Class:</label>
<input type="text" name="class" ><br>
</div>
<div class="roll">
<label>Roll:</label>
<input type="text" name="roll" ><br>
</div>
<div class="section">
<label>Section:</label>
<input type="text" name="section" ><br>
</div>
<div class="dob">
<label>Date of Birth:</label>
<input type="date" name="dob" ><br>
</div>
<div class="joining">
<label>Joining Date:</label>
<input type="date" name="join" ><br>
</div>
<div class="phone">
<label>Phone No:</label>
<input type="text" name="phone" ><br>
</div>
<div class="address">
<label>Address:</label>
<input type="text" name="address" ><br>
</div>
<button type="submit" value="Submit" >Submit</button>
</form>
</div>
<script type="text/javascript">
</script>
</body>
</html>
Thank you for your convenience.
Here is how you possibly can do it.
#app.route("/user/<int:id>")
def user(id):
cur = mysql.connection.cursor()
cur.execute("""SELECT * FROM student_data WHERE id = %s""", (id,))
user = cur.fetchone()
return render_template('user.html', user = user)
Inside user.html
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>User : {{ user.name }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename = 'css/bootstrap.min.css')}}">
<link rel="stylesheet" type="text/css" href="../static/css/add_s_data.css">
<link rel="stylesheet" type="text/css" href="../static/fontawsome/css/all.css">
</head>
<body>
<div class="user">
<p>ID: {{ user.id }}</p>
<p>ID: {{ user.name }}</p>
<p>...</p>
</div>
</body>
</html>
I advise you to go and check Flask-SQLAlchemy, Flask-WTForms they will help you manager database and forms better.

Python selenium iframe [duplicate]

This question already has answers here:
CSS selector for reCAPTCHA checkbok using Selenium and VBA Excel
(1 answer)
Find the reCAPTCHA element and click on it -- Python + Selenium
(2 answers)
How to click on the reCAPTCHA using Selenium and Java
(5 answers)
Closed 3 years ago.
I am trying to switch to iframe in captcha, but I get this error
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//iframe" is: [object HTMLIFrameElement]. It should be an element.
I tryed with switcing to Iiframe, but it doesnt work, I find iframe with inspecting element, but I can not find element when I run the script, also I added explicit wait, time sleep, but it still doesn't work
here is html code
<html lang="en"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Access to this page has been denied.</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet">
<style>
html, body {
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
color: #000;
}
a {
color: #c5c5c5;
text-decoration: none;
}
.container {
align-items: center;
display: flex;
flex: 1;
justify-content: space-between;
flex-direction: column;
height: 100%;
}
.container > div {
width: 100%;
display: flex;
justify-content: center;
}
.container > div > div {
display: flex;
width: 80%;
}
.customer-logo-wrapper {
padding-top: 2rem;
flex-grow: 0;
background-color: #fff;
visibility: hidden;
}
.customer-logo {
border-bottom: 1px solid #000;
}
.customer-logo > img {
padding-bottom: 1rem;
max-height: 50px;
max-width: 100%;
}
.page-title-wrapper {
flex-grow: 2;
}
.page-title {
flex-direction: column-reverse;
}
.content-wrapper {
flex-grow: 5;
}
.content {
flex-direction: column;
}
.page-footer-wrapper {
align-items: center;
flex-grow: 0.2;
background-color: #000;
color: #c5c5c5;
font-size: 70%;
}
#media (min-width: 768px) {
html, body {
height: 100%;
}
}
</style>
<script src="/rw7M6iAV/captcha/PXrw7M6iAV/captcha.js?a=c&m=0&u=266c5db0-19e6-11ea-8150-878a39a29362&v="></script><style type="text/css">.px-loader-wrapper { display: flex;}#keyframes loadingEffect { 0% { background-position: 0; } 100% { background-position: 60vw; }}.px-inner-loading-area { width: 310px; height: 30px; animation-name: loadingEffect; animation-duration: 1.2s; animation-iteration-count: infinite; animation-timing-function: ease; animation-fill-mode: forwards; background: linear-gradient(to right, #F6F6F6 8%, #F0F0F0 18%, #F6F6F6 33%);}.px-loading-area { width: 310px; height: 30px; margin: 0; ;}</style><script src="/rw7M6iAV/init.js"></script><a tabindex="-1" aria-hidden="true" href="/organization/waldeck-associates?_pxhc=1575828688573" rel="nofollow" target="_blank" style="width: 0px; height: 0px; font-size: 0px; line-height: 0;"></a></head>
<body>
<section class="container">
<div class="customer-logo-wrapper">
<div class="customer-logo">
<img src="" alt="Logo">
</div>
</div>
<div class="page-title-wrapper">
<div class="page-title">
<h1>Please verify you are a human</h1>
</div>
</div>
<div class="content-wrapper">
<div class="content">
<div id="px-captcha" role="main"><style>#media screen and (max-width: 350px) {
#px-captcha {
position: relative;
right: 32px;
}
}</style><iframe style="display: block; width: 310px; height: 100px; border: 0; padding:0; margin: 0; -moz-user-select: none;-khtml-user-select: none;-webkit-user-select: none;-ms-user-select: none;user-select: none;" token="efbbf8834a03575dcd9c41ce37a32800bac150d7be5c1b3bf3a953f0879cfc3caa53da776440a3d731e03394daec916c92fe9416ba7947d3cea7370b970a551d"></iframe></div>
<p>
Access to this page has been denied because we believe you are using automation tools to browse the
website.
</p>
<p>
This may happen as a result of the following:
</p>
<ul>
<li>
Javascript is disabled or blocked by an extension (ad blockers for example)
</li>
<li>
Your browser does not support cookies
</li>
</ul>
<p>
Please make sure that Javascript and cookies are enabled on your browser and that you are not blocking
them from loading.
</p>
<p>
Reference ID: #266c5db0-19e6-11ea-8150-878a39a29362
</p>
<div id="px-block-form-wrapper"><div><style>#px-block-form-wrapper{width:400px;position:absolute;bottom:0;left:calc(50% - 200px)}#px-block-toggle-button{height:20px;background:#fff;color:#000;border-radius:3px;padding:10px;cursor:pointer;font-size:13px;text-align:center;width:270px;border:1px solid #000;font-weight:900;margin-left:75px;text-decoration:underline}#px-block-form{background:#fff;border:1px solid;border-color:#404040;color:#000;border-radius:3px;height:265px;font-size:12px}button#px-form-close{float:right;background:0 0;border:none;color:#fff;font-size:18px;cursor:pointer}button#px-form-submit{position:absolute;bottom:10px;background:#404040;border:none;width:100px;border-radius:3px;height:25px;color:#fff;right:20px;cursor:pointer}button#px-form-submit:disabled{background:#ddd;cursor:not-allowed;color:#000}div#px-form{color:#000;padding:15px}div#px-form-head{padding:10px;font-size:14px;background:#404040;color:#fff}div#px-form textarea{margin:-5px 0 0 4px;width:300px;height:36px}div#px-form div{margin-bottom:6px}#px-form-thank-you{background:#fff;color:#000;border:1px solid;border-color:#404040;border-radius:3px;height:54px;padding:5px 10px;text-align:center;width:330px;margin-left:35px}#px-form-thank-you div{font-size:15px;margin-top:14px}#px-form-thank-you span.checkmark{margin-right:8px;color:green;font-size:20px}div#px-form form h4{margin:8px 4px}#media only screen and (orientation:portrait){#px-block-form-wrapper{width:97%;position:relative;left:3%;z-index:10}#px-block-toggle-button{height:20px;background:#fff;color:#000;padding:3px;cursor:pointer;font-size:13px;text-align:center;width:94%;border:none;font-weight:900;text-decoration:underline;margin-left:0;position:fixed;bottom:5px;right:10px;width:initial}#px-block-form{background:#fff;border:1px solid;border-color:#404040;color:#000;border-radius:3px;height:250px;font-size:12px}button#px-form-close{float:right;background:0 0;border:none;color:#fff;font-size:18px;cursor:pointer;position:absolute;right:15px;padding:0;bottom:6px}button#px-form-submit{position:absolute;bottom:10px;background:#404040;border:none;width:100px;border-radius:3px;height:25px;color:#fff;right:20px;cursor:pointer;font-size:13px}button#px-form-submit:disabled{background:#ddd;cursor:not-allowed;color:#000}div#px-form{color:#000;padding:15px}div#px-form-head{padding:10px;font-size:14px;background:#404040;color:#fff;position:relative}div#px-form textarea{margin:-5px 0 0 4px;width:90%;height:36px}div#px-form div{margin-bottom:6px}#px-form-thank-you{background:#fff;color:#000;border:1px solid;border-color:#404040;border-radius:3px;height:54px;padding:5px 10px;text-align:center;width:94%;margin-left:0}#px-form-thank-you div{font-size:15px;margin-top:14px}#px-form-thank-you span.checkmark{margin-right:8px;color:green;font-size:20px}div#px-form form h4{margin:8px 4px}.container{display:block;position:relative}.container .content-wrapper{padding-bottom:40px}.container .page-footer-wrapper{position:fixed;bottom:0}.container .content-wrapper .content{margin:0 auto}}</style><div id="px-block-toggle-button" onclick="_pxToggleOpenForm()" hidden="">Report an issue</div><div id="px-block-form"><div id="px-form-head"><span>Experiencing issues? Let us know:</span> <button onclick="_pxToggleOpenForm(!0)" id="px-form-close">X</button></div><div id="px-form"><form><div style="display:none"><input onchange="_pxItemSelected()" type="radio" id="opt0" name="px-report-reason" value="-1"> <label for="opt0">I’m a bot</label></div><div><input onchange="_pxItemSelected()" type="radio" id="opt1" name="px-report-reason" value="1"> <label for="opt1">I don’t see any challenge</label></div><div><input onchange="_pxItemSelected()" type="radio" id="opt2" name="px-report-reason" value="2"> <label for="opt2">I keep solving the challenge</label></div><div><input onchange="_pxItemSelected()" type="radio" id="opt3" name="px-report-reason" value="3"> <label for="opt3">Other (please elaborate)</label></div><h4>Additional Information:</h4><textarea id="px-form-free-text" placeholder="Experiencing other issues?"></textarea></form><button disabled="disabled" id="px-form-submit" onclick="_pxSubmitForm()">Send</button></div></div><div hidden="" id="px-form-thank-you"><div><span class="checkmark">✓</span><span>Thank you for the feedback</span></div></div></div></div></div>
</div>
<div class="page-footer-wrapper">
<div class="page-footer">
<p>
Powered by
PerimeterX
, Inc.
</p>
</div>
</div>
</section>
<script>
window._pxAppId = 'PXrw7M6iAV';
window._pxJsClientSrc = '/rw7M6iAV/init.js';
window._pxFirstPartyEnabled = true;
window._pxVid = '';
window._pxUuid = '266c5db0-19e6-11ea-8150-878a39a29362';
window._pxHostUrl = '/rw7M6iAV/xhr';
</script>
<script>
var s = document.createElement('script');
s.src = '/rw7M6iAV/captcha/PXrw7M6iAV/captcha.js?a=c&m=0&u=266c5db0-19e6-11ea-8150-878a39a29362&v=';
var p = document.getElementsByTagName('head')[0];
p.insertBefore(s, null);
if (true) {
s.onerror = function () {
s = document.createElement('script');
var suffixIndex = '/rw7M6iAV/captcha/PXrw7M6iAV/captcha.js?a=c&m=0&u=266c5db0-19e6-11ea-8150-878a39a29362&v='.indexOf('captcha.js');
var temperedBlockScript = '/rw7M6iAV/captcha/PXrw7M6iAV/captcha.js?a=c&m=0&u=266c5db0-19e6-11ea-8150-878a39a29362&v='.substring(suffixIndex);
s.src = '//captcha.px-cdn.net/PXrw7M6iAV/' + temperedBlockScript;
p.parentNode.insertBefore(s, p);
};
}
</script>
</body></html>
The exception itself says that the selector is incorrect. Try to use absolute xpath while selecting the iFrame. Absolute xpath for captcha iframe always works for me.
Also it woulbe be great if you can share your automation code

Aligning posts in a django project + mdl

I am making a blog with django but when I try to align them next to each other,
they appear under each other.
the posts appear under each other.
how can i make them horizontally aligned.
here is my html:
{% extends "blog/base.html" %}
{% load blog_tags %}
{% block content %}
<div class="mdl-layout mdl-js-layout ">
{% for post in posts %}
<main class="mdl-layout__content">
<div class="mdl-grid ">
<div class="mdl-card mdl-cell mdl-cell--6-col mdl-cell--4-col mdl-shadow--2dp">
<figure class="mdl-card__media">
<img src="{{ post.photo }}" alt="" />
</figure>
<div class="mdl-card__title">
<h1 class="mdl-card__title-text">{{ post.title }}</h1>
</div>
<div class="mdl-card__supporting-text">
<p>{{ post.body|truncatewords:11 }}</p>
</div>
<div class="mdl-card__actions mdl-card--border">
<a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect kal" href="{{ post.get_absolute_url }}">Read More</a>
<div class="mdl-layout-spacer"></div>
<button class="mdl-button mdl-button--icon mdl-button--colored kal"><i class="material-icons">favorite</i></button>
<button class="mdl-button mdl-button--icon mdl-button--colored kal"><i class="material-icons">share</i></button>
</div>
</div>
{% endfor %}
</div>
</div>
</main>
</div>
{% endblock %}
css file:
I think the problem is with this css file but i had trouble finding it
css file:
I think the problem is with this css file but i had trouble finding itcss file:
I think the problem is with this css file but i had trouble finding itcss file:
I think the problem is with this css file but i had trouble finding itcss file:
I think the problem is with this css file but i had trouble finding itcss file:
I think the problem is with this css file but i had trouble finding it
body{
background: white;
}
.mdl-grid {
max-width: 600px;
}
.mdl-card__media {
margin: 0;
}
.mdl-card__media > img {
max-width: 100%;
}
.mdl-card__actions {
display: flex;
box-sizing:border-box;
align-items: center;
}
.mdl-card__actions > .mdl-button--icon {
margin-right: 3px;
margin-left: 3px;
}
.kal:hover{
text-decoration: none;
}
.main{
font-family: 'Bungee', cursive;
font-size: 600%;
color: white;
border: None;
position: absolute;
bottom: 9%;
left: 6%;
}
.skills{
position: absolute;
bottom: 5%;
left: 6%;
color: white;
}
.iff{
position: absolute;
bottom: 9%;
right: 9%;
}
.ten{
color: black;
background: white;
font-size: 300%;
border-radius: 300px;
margin: 10px;
padding: 30px;
}
.navbar{
border-radius: 0px !important;
background: transparent;
width: 100% !important;
border: None !important;
font-family: 'Josefin Sans', sans-serif;
position: fixed !important;
width: 100%;
z-index: 1 !important;
}
.nad{
font-size: 120% !important;
}
li:hover{
font-weight: bold;
background: white;
}
.navbar-brand{
font-family: 'Josefin Sans', sans-serif;
font-weight: bold;
font-size-adjust: auto;
font-size: 200% !important;
color: black !important;
}
header{
background: url("https://wp-themes.com/wp-content/themes/twentyseventeen/assets/images/header.jpg");
background-attachment: fixed;
height: 100vh;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
.tn{
background: white;
border-radius: 250px;
color: black;
}
.main1{
font-family: 'Sedgwick Ave Display', cursive;
}
.tab{
font-family: 'Abril Fatface', cursive;
}
.tab12{
font-family: 'Abril Fatface', cursive;
}

Categories

Resources