有没有一种方法可以通过使用 POST 变量来分配 Mako 模板内的图像 src?
Posted
技术标签:
【中文标题】有没有一种方法可以通过使用 POST 变量来分配 Mako 模板内的图像 src?【英文标题】:Is there a possible way to assign the src of images inside a Mako template by using POST variables? 【发布时间】:2020-04-23 01:32:43 【问题描述】:我目前被困在这个问题上,我似乎无法弄清楚......我已经使用 CherryPy 创建了一个小型 web 应用程序,并且我已经使用 Mako 作为 html 模板。一切都与一个包含汽车列表的 mysql 数据库相关联。我的问题如下:我正在使用 POST 表单从主页导航到汽车部分并指定某个汽车品牌。到达该页面后,我根据 POST 中的标准从数据库加载所有汽车。该变量包含从数据库中提取的所有行。然后我继续遍历行列表以显示每辆汽车的信息等,但我似乎无法找到将每张图片与其对应的汽车联系起来的方法。我的想法是生成我的 img 文件夹( ../img/cars/ )的路径并将其与汽车 ID + 文件扩展名连接起来,但这似乎不起作用......(我的图片保存为 1 .jpg、2.jpg 等 - 每个数字代表汽车 ID)。
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="../css/style.css" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<div class="topnav" id="myTopnav">
<a href="index" id="logolink">
<img src="../img/logo_transparent.png" >
</a>
<a href="index" id="link" class="active">Home</a>
<a href="#news" id="link">News</a>
<a href="contact" id="link">Contact</a>
<a href="about" id="link">About</a>
</div>
<div class ="pageinfo">
<img src="../img/slides/slide1.jpg" >
<img src="../img/slides/slide2.jpg" >
</div>
<div class="pagecontent">
<h1>Show cars by Category</h1>
<div class="row">
<div class="column">
<div class="card">
<img src="../img/categories/hatchback.png" style="width:200px; height:200px;">
<div class="container">
<h2>Hatchback</h2>
<p>Agile due its reduced size.</p>
<form action="displaybycategory" method="POST">
<p><button class="button" type="submit" name="category" value="hatchback" >Check available models</button></p>
</form>
</div>
</div>
</div>
<div class="column">
<div class="card">
<img src="../img/categories/suv.png" style="width:200px; height:200px;">
<div class="container">
<h2>SUV</h2>
<p>Extremely versatile due to its power and utility.</p>
<form action="displaybycategory" method="POST">
<p><button class="button" type="submit" name="category" value="suv" >Check available models</button></p>
</form>
</div>
</div>
</div>
<div class="column">
<div class="card">
<img src="../img/categories/sedan.png" style="width:200px; height:200px;">
<div class="container">
<h2>Sedan</h2>
<p>Enough space to fit all needs.</p>
<form action="displaybycategory" method="POST">
<p><button class="button" type="submit" name="category" value="sedan" >Check available models</button></p>
</form>
</div>
</div>
</div>
</div>
<div class="row">
<div class="column">
<div class="card">
<img src="../img/categories/convertible.png" style="width:200px; height:200px;">
<div class="container">
<h2>Convertible</h2>
<p>Gives you all the freedom you deserve.</p>
<form action="displaybycategory" method="POST">
<p><button class="button" type="submit" name="category" value="convertible" >Check available models</button></p>
</form>
</div>
</div>
</div>
<div class="column">
<div class="card">
<img src="../img/categories/coupe.png" style="width:200px; height:200px;">
<div class="container">
<h2>Coupe</h2>
<p>Sporty looks, aggressive driving.</p>
<form action="displaybycategory" method="POST">
<p><button class="button" type="submit" name="category" value="coupe" >Check available models</button></p>
</form>
</div>
</div>
</div>
<div class="column">
<div class="card">
<img src="../img/categories/sports.png" style="width:200px; height:200px;">
<div class="container">
<h2>Sports</h2>
<p>City conqueror, track monster.</p>
<form action="displaybycategory" method="POST">
<p><button class="button" type="submit" name="category" value="sports" >Check available models</button></p>
</form>
</div>
</div>
</div>
</div>
<h1>Show cars by Make</h1>
<form action="displaybymake" method="POST">
<table style="width:100%" id="carmakes">
<tr>
<td><p><button class="button" type="submit" name="make" value="Audi" >Audi</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Fiat" >Fiat</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Hyundai" >Hyundai</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Mazda" >Mazda</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Nissan" >Nissan</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Toyota" >Toyota</button></p></td>
</tr>
<tr>
<td><p><button class="button" type="submit" name="make" value="BMW" >BMW</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Ford" >Ford</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Kia" >Kia</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Mercedes-Benz" >Mercedes-Benz</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Porsche" >Porsche</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Volkswagen" >Volkswagen</button></p></td>
</tr>
<tr>
<td><p><button class="button" type="submit" name="make" value="Chevrolet" >Chevrolet</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Honda" >Honda</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Range Rover" >Range Rover</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Mitsubishi" >Mitsubishi</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Subaru" >Subaru</button></p></td>
<td><p><button class="button" type="submit" name="make" value="Volvo" >Volvo</button></p></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
carcategory.html
<!DOCTYPE html>
<html>
<link href="../css/carcategory.css" rel="stylesheet">
<div class="uppercontent">
<a href="index" id="logolink">
<img src="../img/logo_transparent.png" >
</a>
<h2>Current category: $categ</h2>
<hr>
</div>
<table><tr>
% for a in mydata:
% if loop.index % 3 == 0:
</tr><tr><td>
<div class="card" style="margin-left: 125px;">
<img src="../img/cars/1.jpg" style="width:200px; height: 200px;">
<h1>$a[1]</h1>
<p>$a[2]</p>
<p class="price">EUR $a[4]</p>
<p><button>Check it out</button></p>
</div>
</td>
% elif loop.index %3 != 0:
<td>
<div class="card">
<img src="../img/cars/1.jpg" style="width:200px; height: 200px;">
<h1>$a[1]</h1>
<p>$a[2]</p>
<p class="price">EUR $a[4]</p>
<p><button>Check it out</button></p>
</div>
</td>
% endif
% endfor
</html>
start.py:
import cherrypy
import mysql.connector
from mysql.connector import errorcode
import os
from mako.template import Template
from mako.lookup import TemplateLookup
path = os.path.abspath(os.path.dirname(__file__))
lookup = TemplateLookup(directories=[os.path.join(path, 'html')])
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="luk610",
database="siteril"
)
class MainPage(object):
@cherrypy.expose
def index(self):
template = lookup.get_template('index.html')
return template.render()
index.exposed = True
@cherrypy.expose
def displaybycategory(self, category):
template = lookup.get_template('carcategory.html')
mycursor = mydb.cursor()
payload = 'SELECT * FROM cars WHERE category=' + '"' + category + '"'
mycursor.execute(payload)
result = mycursor.fetchall()
return template.render(mydata=result, categ=category)
displaybycategory.exposed = True
@cherrypy.expose
def displaybymake(self, make):
template = lookup.get_template('carmake.html')
mycursor = mydb.cursor()
payload = 'SELECT * FROM cars WHERE make=' + '"' + make + '"'
mycursor.execute(payload)
result = mycursor.fetchall()
return template.render(mydata=result)
displaybycategory.exposed = True
@cherrypy.expose
def about(self):
template = lookup.get_template('about.html')
return template.render()
about.exposed = True
@cherrypy.expose
def contact(self):
template = lookup.get_template('contact.html')
return template.render()
contact.exposed = True
if __name__ == '__main__':
conf_path_root = os.path.dirname(os.path.abspath(__file__))
conf_path = os.path.join(conf_path_root, "config.conf")
cherrypy.config.update(conf_path)
cherrypy.tree.mount(MainPage(), '/', config=conf_path)
cherrypy.engine.start()
cherrypy.engine.block()
这应该是这样的:src="../img/cars/a[0].jpg",其中 a[0] 获取汽车 ID
如果我没有包含任何内容或没有澄清问题,我深表歉意。我是这个东西的完整初学者,任何帮助/建议将不胜感激。谢谢!
【问题讨论】:
【参考方案1】:我发现的唯一临时解决方案是通过多个 if 语句对图片选择进行硬编码......(是的,我知道...... :D)就像这样:
% if a[0] == 1:
<img src="../img/cars/1.jpg" style="width:200px; height: 200px;">
% elif a[0] == 2:
<img src="../img/cars/2.jpg" style="width:200px; height: 200px;">
% elif a[0] == 3:
<img src="../img/cars/3.jpg" style="width:200px; height: 200px;">
% elif a[0] == 4:
<img src="../img/cars/4.jpg" style="width:200px; height: 200px;">
% elif a[0] == 5:
<img src="../img/cars/5.jpg" style="width:200px; height: 200px;">
% elif a[0] == 6:
<img src="../img/cars/6.jpg" style="width:200px; height: 200px;">
% elif a[0] == 7:
<img src="../img/cars/7.jpg" style="width:200px; height: 200px;">
% elif a[0] == 8:
<img src="../img/cars/8.jpg" style="width:200px; height: 200px;">
% elif a[0] == 9:
<img src="../img/cars/9.jpg" style="width:200px; height: 200px;">
% elif a[0] == 10:
<img src="../img/cars/10.jpg" style="width:200px; height: 200px;">
% elif a[0] == 11:
<img src="../img/cars/11.jpg" style="width:200px; height: 200px;">
% elif a[0] == 12:
<img src="../img/cars/12.jpg" style="width:200px; height: 200px;">
% elif a[0] == 13:
<img src="../img/cars/13.jpg" style="width:200px; height: 200px;">
% elif a[0] == 14:
<img src="../img/cars/14.jpg" style="width:200px; height: 200px;">
% elif a[0] == 15:
<img src="../img/cars/15.jpg" style="width:200px; height: 200px;">
% elif a[0] == 16:
<img src="../img/cars/16.jpg" style="width:200px; height: 200px;">
% elif a[0] == 17:
<img src="../img/cars/17.jpg" style="width:200px; height: 200px;">
% elif a[0] == 18:
<img src="../img/cars/18.jpg" style="width:200px; height: 200px;">
% elif a[0] == 19:
<img src="../img/cars/19.jpg" style="width:200px; height: 200px;">
% elif a[0] == 20:
<img src="../img/cars/20.jpg" style="width:200px; height: 200px;">
%endif
我知道这很糟糕,但我似乎仍然无法找到一种方法来利用这些变量来将它们用作 src 的属性。
【讨论】:
【参考方案2】:我认为您应该尝试使用 javascript 来替代您的问题。
还强烈建议使用字符串连接运算符(+, +=)
而不是其他方法来执行此操作。
【讨论】:
【参考方案3】:在 HTML 中使用变量的 mako 语法如下:
<img src=$filename />
从包含该模板的模板接收变量文件名并将文件名作为变量传递。或者在模板中动态设置,在模板中运行 python 代码使用这个:
filename = "/var/www/myproject/path" + "resources"
【讨论】:
以上是关于有没有一种方法可以通过使用 POST 变量来分配 Mako 模板内的图像 src?的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法使用三元运算符 - 或类似的方法 - 来选择要分配的变量?
有没有办法检索分配给 TableLayout 对象的变量名称以从另一个对象中识别?