python - 如何在 Python 中使用 Django 让 html/css 框架更快?
问题描述
我已经为 html 和 css 单独完成了非常繁琐的任务来制作 pH 晴雨表,但我发现 Python Django 有一个功能可以在几秒钟内完成这项工作。你是怎样做的?
<!DOCTYPE html>
<html>
<head>
<title>Color</title>
<style type="text/css">
body {
background-color: silver;
color: white;
padding: 20px;
font-family: Arial, Verdana, sans-serif;}
h1 {
background-color: #fffffff;
background-color: hsla(0,100%,100%,0.5);
color: #64645A;
padding: inherit;}
p {
padding: 5px;
margin: 0px;}
p.zero{
background-color: rgb(238,62,128);}
p.one {
background-color: rgb(244,90,139);}
p.two {
background-color: rgb(243,106,152);}
p.three {
background-color: rgb(244,123,166);}
p.four {
background-color: rgb(245,140,178);}
p.five {
background-color: rgb(245,159,192);}
p.six {
background-color: rgb(245,176,204);}
p.seven {
background-color: rgb(0,187,136);}
p.eight {
background-color: rgb(140,202,242);}
p.nine {
background-color: rgb(114,193,240);}
p.ten {
background-color: rgb(84,182,237);}
p.eleven {
background-color: rgb(48,170,233);}
p.thirteen {
background-color: rgb(0,160,230);}
p.fourteen {
background-color: rgb(0,136,221);}
</style>
</head>
<body>
<h1>pH Scale</h1>
<p class="fourteen">14.0 VERY ALKALINE</p>
从这个来源Django和Html有什么区别?
我必须在 Django 中找到什么功能或设置才能在几秒钟内制作任何 html/css 框架,因为仅 html 和 css 来制作这样的框架是一项非常繁琐的任务?
解决方案
您有许多可用的解决方案。例如,在您的情况下,您可以创建一个模板标签以在您的 base.html 模板中生成 CSS,该模板使用 .html 为您生成 CSS for
。
查看有关如何创建简单标签的示例:https ://docs.djangoproject.com/en/2.1/howto/custom-template-tags/#simple-tags
问题是您没有在答案中添加详细信息,是获取数据以创建 RGB 值。如果 RGB 与您在模板中渲染的模型相关,您可以使用inline style CSS
最后一个解决方案可以使用模板标签完成,该标签可以根据对象中的数据以 CSS 样式呈现您的对象。
@register.inclusion_tag('element.html')
def render_element(element *args, **kwargs):
context = {}
# here based on your element you can decide what to add
# in the context. You can also create `inline css` that your
# template will render as you want.
...
return context
在此处查看详细信息:https ://docs.djangoproject.com/en/2.1/howto/custom-template-tags/#inclusion-tags
您的问题严格来说是一个渲染问题,所以我会避免涉及模型。