部署项目时出现客户端不能访问

报错信息: django You may need to add ‘123.57.71.226’ to ALLOWED_HOSTS

settings.py

  1. ALLOWED_HOSTS = ['*'] # 允许所有主机访问

关闭Django的Post认证


django_project/settings.py

  1. MIDDLEWARE = [
  2. 'django.middleware.security.SecurityMiddleware',
  3. 'django.contrib.sessions.middleware.SessionMiddleware',
  4. 'django.middleware.common.CommonMiddleware',
  5. # 'django.middleware.csrf.CsrfViewMiddleware', 关闭POST校验
  6. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  7. 'django.contrib.messages.middleware.MessageMiddleware',
  8. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  9. ]

也可以再html from表单新增配置
django开启post校验 {% csrf_token %}

开启Django修改配置后自动重启功能

settings.py

  1. # 注释原始配置
  2. # TEMPLATES = [
  3. # {
  4. # 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  5. # 'DIRS': [os.path.join(BASE_DIR, 'templates')]
  6. # ,
  7. # 'APP_DIRS': True,
  8. # 'OPTIONS': {
  9. # 'context_processors': [
  10. # 'django.template.context_processors.debug',
  11. # 'django.template.context_processors.request',
  12. # 'django.contrib.auth.context_processors.auth',
  13. # 'django.contrib.messages.context_processors.messages',
  14. # ],
  15. # },
  16. # },
  17. # ]
  18. # 新的配置
  19. TEMPLATES = [
  20. {
  21. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  22. 'DIRS': [os.path.join(BASE_DIR, 'templates')]
  23. ,
  24. 'APP_DIRS': False, # 改为False
  25. 'OPTIONS': {
  26. 'context_processors': [
  27. 'django.template.context_processors.debug',
  28. 'django.template.context_processors.request',
  29. 'django.contrib.auth.context_processors.auth',
  30. 'django.contrib.messages.context_processors.messages',
  31. ],
  32. 'loaders': [ # 新增
  33. 'django.template.loaders.filesystem.Loader',
  34. 'django.template.loaders.app_directories.Loader',
  35. ],
  36. },
  37. },
  38. ]