"""
Django settings for myfirstdjango project.
Generated by 'django-admin startproject' using Django 3.1.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os #引入os模块
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent #BASE_DIR是项目根目录
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'hgs^kkkbeebknjc(!&mc6*+3-qj37^cv-$h-7=cbrhj#5(5b3y'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True #DEBUG = True表示是调试模式,上线的时候改为False
ALLOWED_HOSTS = ['*'] #设置为'*'表示允许所有IP访问
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'myfirstdjango.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')], #这里放HTML文件的路径。
'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',
],
},
},
]
WSGI_APPLICATION = 'myfirstdjango.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = { #数据库配置
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us' #语言设置,可以改为zh-hans表示中文
TIME_ZONE = 'UTC' #时区设置,可以改为Asia/Shanghai
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/' #静态文件的别名叫static
改变settings.py文件为上所示,之后再次启动Django项目,可以看到,页面如下所示。
"""myfirstdjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.shortcuts import HttpResponse #导入HttpResponse
#路由所对应的API
def index(request):
pass
return HttpResponse("Hello World!") #HttpResponse是返回字符串,此处返回Hello World
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',index), #增加路由
]
更改urls.py文件之后,然后访问地址:http://81.68.194.141/index/,页面显示如下。
接下来,尝试返回一个HTML页面,下面来更改urls.py文件来返回一个HTML页面。
from django.contrib import admin
from django.urls import path
from django.shortcuts import HttpResponse,render #引入HttpResponse,render
def index(request):
pass
#return HttpResponse("Hello World!")
return render(request,'index.html') #render是用来返回页面的,此处返回HTML页面
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',index),
]
index.html文件内容
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<title>第一个Django程序</title>
<h1>你好,世界!</h1>
</body>
</html>
index.html需要放在templates目录下(该目录需要手动创建,该目录就是settings.py文件中TEMOLATES中的DIRS所设置的目录。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')], #这里放HTML文件的路径。
'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',
],
},
},
]
现在整个项目的结构如下图所示:
现在,访问页面,会发现,页面如下所示。
本篇到此结束,大概介绍了settings和urls的作用。