urls.py 文件
"""mysite 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 . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
# path('网址中端口号后面追加的内容', '文件名.函数名', name='函数名')
]
urlpatterns中,添加程序启动运行的功能文件。path()中,
第一个参数:浏览器网址中,端口号后面追加的内容。
当为空时,网址为: http://127.0.0.1:8000/
当为‘admin/’时,网址为:http://127.0.0.1:8000/admin/
第二个参数:文件名.函数名。
我的后台逻辑写到views.py文件中的home函数中,那么这里应该是, views.home。
views.py文件如下,其中,index.html为我要调用的html文件
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return render(request, 'index.html')
# def home(request):
# return HttpResponse("Hello, world. You're at the polls index.")
第三个参数:即name,一般设置为函数名即可。
settings.py 文件:
# SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['10.193.33.139']
这是设置是否开启测试模式,默认DEBUG=True。当DEBUG=False时,ALLOWED_HOSTS不能为空。ALLOWED_HOSTS列表默认为空。当启动服务时,我想修改默认ip地址(默认为127.0.0.1)时,比如修改为我本机地址(10.192.11.11)。那么我需要在此列表中加入我本机的IP地址,同时启动命令为:python manage.py runserver 10.192.11.11:8080
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(base_DIR, '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',
],
},
},
]
这里面DIRS列表,就是存放html文件的路径。比如我的index.html 文件放在项目根目录下的html文件夹内,那这里就是“os.path.join(base_DIR, 'html/')”
STATICFILES_DIRS = (
os.path.join(base_DIR, 'html/static'),
)
“STATICFILES_DIRS”参数用来设置静态文件存放地址,比如html文件用到的各种css、js文件等。
LANGUAGE_CODE = 'en-us' # TIME_ZONE = 'UTC' TIME_ZONE = 'Asia/Shanghai'
这是设置服务的时区以及语言
参考:Writing your first Django app, part 1 | Django documentation | Django (djangoproject.com)https://docs.djangoproject.com/en/3.2/intro/tutorial01/



