标签:ons shortcut root ssi files session host .sh icp
根目录文件:hello_pycharm [__init__.py __pycache__ settings.py urls.py wsgi.py]
App:hello [admin.py apps.py __init__.py migrations models.py tests.py urls.py views.py __pycache__ ]
模板文件:templates
根路由urls中:
from django.conf.urls import url,include
from django.contrib import admin
from hello import views
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^hi/‘,views.hi),
url(r‘^hell/‘,views.hello_use_template),
url(r‘^hello/‘,include("hello.urls"))
]
App文件夹hello中的子路由urls中:一个应用app可以有一个路由 称为主应用的子路由也叫二级路由
urlpatterns = [
url(r‘^a/‘,views.hi),
url(r‘^b/‘,views.hello_use_template)
]
views中:
from django.shortcuts import render
from django.http import HttpResponse
def hello_use_template(request):
return render(request,"hello.html",context={"name":"Gavin"})
def hi(request):
html = ‘‘‘<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
‘‘‘
return HttpResponse(html)
模板(templates)中:hello.html
<!DOCTYPE html>
<html lang=‘en‘>
<head>
<meta charset=‘UTF-8>
<title>Title</title>
</head>
<body>
<h1>Hello {{name}}!</h1>
</body>
</html>
Settings中:
import os
print(__file__)
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY= ‘ja=_kx#x^i0swyl_1b0f8*%p^8g5pj)d2!ovwk7hpopnqyaa‘
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘hello.apps.HelloConfig‘,或者用app.py文件的name值简写:‘hello’
]
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 = ‘hello_pycharm.urls‘
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‘,
],
},
},
]
WSGI_APPLICATION = ‘hello_pycharm.wsgi.application‘
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.sqlite3‘,
‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
}
}
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‘,
},
]
LANGUAGE_CODE = ‘zh-Hans‘
TIME_ZONE = ‘UTC‘
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = ‘/static/‘
print(BASE_DIR)
print(os.path.join(BASE_DIR, ‘templates‘))
路由(b)经过模板渲染输出结果:
127.0.0.1:8000/hello/b/
‘’Hello August !”
标签:ons shortcut root ssi files session host .sh icp
原文地址:https://www.cnblogs.com/liurwei/p/9446284.html