Q
Q
qvarkk2021-11-06 08:34:55
Django
qvarkk, 2021-11-06 08:34:55

In order for a dynamic variable to take on a new value, you have to restart the server in Django, how to solve it?

I recently started diving into programming in general, and I ran into a problem in Django: following the tutorial, I set a variable that should be passed to the html page, everything seems to be fine, but exactly until the variable value changes, the previously set variable is displayed on the page (or if the variable is only set, then nothing is displayed), and in order for it to be displayed, you have to restart the

views.py server

from django.shortcuts import render

# Create your views here.
def index(request):
    context = {
        'name': 'Nick',
        'age': 24
    }
    return render(request, 'index.html', context)


index.html
<h1>Hi, {{name}}<br>You are {{age}} years old</h1>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Karabanov, 2021-11-06
@qvarkk

It is logical, because these are not dynamic variables, but hard-coded ones.
Gunicorn has the ability to autoload if the code has changed, you can use it.

V
Vadim, 2021-11-08
@Viji

Use the values ​​from the file (env) with environment variables and the dotenv package, try to rewrite your file with new variable values
​​https://stackoverflow.com/questions/63837315/chang...

from django.shortcuts import render

# Create your views here.
def index(request):
    context = {
        'name':  var_name,
        'age':  var_age
    }
    return render(request, 'index.html', context)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question