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

Question:

I have created an app using Django, I have a variable that I set using a form for more security. 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

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

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.

I check the values and it is "YYY".

I check again after an hour and it is equal to "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.

Asked By: Rahim

||

Answers:

Every time a user goes to your page, views.py will be run, and w_m will be set to "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.

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.

Answered By: raphael

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)
Answered By: Rahim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.