Skip to content
Advertisement

Problem with Django does not detect change in python code every time need to restart the server (problem in runserver)

I have a problem my django server does not show the updated python code when refreshing the page everytime i need to restart the server i searched the internet and all what i found is when i run the server

./manage.py runserver

it automatically detect any change in the code but i use it and it does not detect the change any help

def index(request):
    name = "David"
    return render(request, "index.html", {"name": name})

HTML file

<!DOCTYPE html>
<html>
    <head>
        <h1> How are you doing today {{name}} </h1>
    </head>
    <body>

    </body>
</html>

settings.py

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp', # this is myapp that have the index function
]

Django Version

Django==3.2.8
pytz==2021.3

every time i refresh it shows David D the previous value of name not the new one i have to restart the server to show the new value

Also when changing the HTML code it detect the change and just refreshing the page is sufficient(no need to restart the server) for python it is another story it does not detect any change (need to restart the server to detect the change)

i also tried making different projects

update: I tried another Django version Django==2.1.5 and it works fine I really do not know the cause of the problem but changing the version seems to work

update 2: I have tested the 3.2.8 version of django on another PC and it is working fine so i do not know where is the problem

Advertisement

Answer

I just found the reason for this problem in my settings.py file

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

I was doing

'DIRS': [BASE_DIR, "templates"]

Now after using

'DIRS': [os.path.join(BASE_DIR, "templates")]

It is working fine Django version == 3.2.8 in this version do not forget to import os and for the other versions I used os.path.join so that explains why it was working, beside if you are doing the urls.py files well it will work even without including the templates in DIR list

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement