簡體   English   中英

每次需要重新啟動服務器時,Django 都沒有檢測到python 代碼的變化(runserver 中的問題)

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

我有一個問題,我的 django 服務器在每次需要重新啟動服務器時刷新頁面時不顯示更新的 python 代碼我搜索了互聯網,我發現的所有內容都是在我運行服務器時

./manage.py runserver

它會自動檢測代碼中的任何更改,但我使用它並沒有檢測到任何幫助的更改

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

HTML文件

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

    </body>
</html>

設置.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 版本

Django==3.2.8
pytz==2021.3

每次我刷新它都會顯示 David D 以前的 name 值而不是新的我必須重新啟動服務器才能顯示新值

此外,當更改 HTML 代碼時,它會檢測到更改,並且只需刷新頁面就足夠了(無需重新啟動服務器)對於 python,它是另一個故事,它不會檢測到任何更改(需要重新啟動服務器以檢測更改)

我也嘗試制作不同的項目

更新:我嘗試了另一個 Django 版本Django==2.1.5 ,它工作正常我真的不知道問題的原因,但更改版本似乎有效

更新 2:我已經在另一台 PC 上測試了 3.2.8 版本的 django,它工作正常,所以我不知道問題出在哪里

我剛剛在我的 settings.py 文件中找到了這個問題的原因

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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',
            ],
        },
    },
]

我在做

'DIRS': [BASE_DIR, "templates"]

現在使用后

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

它工作正常 Django 版本 == 3.2.8 在這個版本中不要忘記導入 os 和其他版本我使用 os.path.join 這樣解釋了它為什么工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM