简体   繁体   English

用表格设置变量,一段时间后返回初始值

[英]Setting the variable with the form and return to the initial value after some time

I have created an app using Django, I have a variable that I set using a form for more security.我使用 Django 创建了一个应用程序,我使用表单设置了一个变量以提高安全性。 But after a few hours, I check its value and see that it is zero.但是几个小时后,我检查了它的值,发现它是零。 Every time I set the value, after a while the value becomes zero I have published this app on the host每次我设置值时,一段时间后值变为零我已经在主机上发布了这个应用程序

view.py视图.py

w_m = "xxx"
def admin_setbasedata(request):
    form_ = admin_form()
    global w_m
        if request.method == "POST" and "set_w" in request.POST:
            w_m=request.POST.get('serial')

form.py表格.py

class admin_form(forms.Form):
     serial = forms.CharField(widget=forms.TextInput(attrs={'class':'form-input','oninput':'w_t(this)', 'id': 'serial','autocomplete':"off"}),)

I make variable w_m equal to "YYY" using the form.我使用表格使变量 w_m 等于“YYY”。

I check the values and it is "YYY".我检查了这些值,它是“YYY”。

I check again after an hour and it is equal to "xxx".一小时后我再次检查,它等于“xxx”。

It seems that every time the site is visited, view.py is executed from the beginning and the variables are reset to their initial values.似乎每次访问该站点时,都会从头开始执行 view.py 并将变量重置为其初始值。

Every time a user goes to your page, views.py will be run, and w_m will be set to "xxx" .每次用户访问您的页面时,views.py 都会运行,并且w_m将设置为"xxx" So when you say,所以当你说,

It seems that every time the site is visited, view.py is executed from the beginning and the variables are reset to their initial values.似乎每次访问该站点时,都会从头开始执行 view.py 并将变量重置为其初始值。

That is what is supposed to happen.这就是应该发生的事情。 If you need to have a variable that is maintained like what I believe you are trying to do with the global identifier, you need to put that into your database.如果您需要像我认为您正在尝试使用global标识符那样维护变量,则需要将其放入数据库中。

It is better to import the variables from the file and record the changes in the file最好从文件中导入变量并记录文件中的变化

w_m = "YYYY"
with open('myvar.py', 'w') as file:
     file.write("w_m = " + w_m)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM