Django 前后端不分离 代码结构详解
2018-08-10 11:27:07来源:博客园 阅读 ()
Demo: hello_pycharm
根目录文件: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 !”
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:Django中ORM简述
- Django项目中使用qq第三方登录。 2019-08-13
- Django和前端用ajax传输json等数据 2019-08-13
- Django基本知识 2019-08-13
- JavaScript获取Django模板中指定键值的数据,使用过滤器 2019-08-13
- django框架使用及创建项目 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash