我想在我的页面上创建一个按钮,单击该按钮将随机带您进入在我的网站上查看的模型
在我的urls.py中,我已经定义了
def convert_time_zone(passed_row):
change_date = passed_row
print(str(change_date))
tz = time_zone
my_time_list = list(change_date)
counter = 0
for i in my_time_list:
if i == ":":
print("Found : Counter is " + str(counter))
print("\nBEFORE: " + str(''.join(my_time_list)) + "\n")
if my_time_list[counter - 1] == "9":
my_time_list[counter - 1] = str(tz - 1)
if my_time_list[counter - 2] == " ":
my_time_list[counter - 2] = " 1"
elif my_time_list[counter - 2] != " ":
my_time_list[counter - 2] = " " + str(int(my_time_list[counter - 2]) + tz)
print("AFTER: " + str(''.join(my_time_list)) + "\n")
elif my_time_list[counter - 1] == "2" and my_time_list[counter - 2] == "1" and tz > 0:
my_time_list[counter - 1] = str(tz)
my_time_list[counter - 2] = " "
print("AFTER: " + str(''.join(my_time_list)) + "\n")
else:
my_time_list[counter - 1] = str(int(my_time_list[counter - 1]) + tz)
print("AFTER: " + str(''.join(my_time_list)) + "\n")
my_time_list = str(''.join(my_time_list))
print(my_time_list)
break
counter += 1
return my_time_list
因此在模板中放置
之类的内容> path('<int:id>/workout/', views.workout, name='workout')
工作正常,但是我想用一个随机整数替换那个“ 1”
我尝试用不同的方式来定义变量,但没有成功
我尝试过从视图传递ID数组,并使用
<a href="{% url 'workout' 1 %}">Get a random workout</a>
但是url方法不想接受这个
答案 0 :(得分:1)
使用Python的 random
模块获取随机数,并将其作为上下文数据传递到模板
#views.py
from random import randint
def test_view(request, slug=None):
return render(request, 'random.html', {"random_int": randint(1, 99999)})
,然后在模板中将 random_int
用作
<a href="{% url 'password_change_done' random_int %}">Get a random workout</a>