Jos*_*ous 3 html python django
好的,所以我正在尝试使用Django/Python创建一个随机数生成器网页.我需要完成的是以某种方式在我的HTML模板文件中使用python代码,除了我无法找到如何做到这一点.
<h1 style="font-size:50px;line-height:20px;color:rgb(145,0,0);font- family: Arial Black, Gadget, sans-serif"></h1>
<h2 style="line-height:10px;color:rgb(140,140,140)"></h2>
<h3 style="font-size:40px;line-height:10px;font-family: Arial Black, Gadget, sans-serif"></h3>
<body style="background-color:rgb(255,239,154)"></body>
<!--Style placeholders-->
<h1 style="text-align:center;position:relative;top:20px">
Test Site
</h1>
<!--Reroll icon-->
<h1 style="text-align:center;position:relative;top:20px">
<input type='image' style='width:60px;height:56px;' src='../../static/polls/dice.png' alt='Re-roll'
onclick='location.reload();' value='Roll' /></h1>
Run Code Online (Sandbox Code Playgroud)
drd*_*man 10
没有内置的方法来做到这一点.如果您只需要一个随机值,一次,并且您不想从视图函数传递它 - 我想自定义模板标记就是这样.
在任何适当的应用程序中,使用以下内容创建一个文件templatetags/random_numbers.py(templatetags/__init__.py如果没有任何其他自定义模板标记,则为空):
import random
from django import template
register = template.Library()
@register.simple_tag
def random_int(a, b=None):
if b is None:
a, b = 0, a
return random.randint(a, b)
Run Code Online (Sandbox Code Playgroud)
然后,在您的模板中使用它如下:
{% load random_numbers %}
<p>A random value, 1 ? {% random_int 1 10 %} ? 10.</p>
Run Code Online (Sandbox Code Playgroud)
有关自定义标记的更多文档:https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/
| 归档时间: |
|
| 查看次数: |
5917 次 |
| 最近记录: |