我有一个问题,我的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>
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版本
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': [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"]
现在使用
'DIRS': [os.path.join(BASE_DIR, "templates")]
工作正常Django version == 3.2.8在这个版本中不要忘记导入os对于其他版本,我使用的是os.path.join所以这就解释了为什么它能工作,如果你正在做urls.py文件,它即使不包括DIR列表中的模板也能工作