zoukankan      html  css  js  c++  java
  • Django 前后端不分离 代码结构详解

    Demo:  hello_pycharm

    根目录文件:hello_pycharm [__init__.py  __pycache__  settings.py  urls.py  wsgi.py]

    Apphello [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 !

  • 相关阅读:
    VS2008 环境中完美搭建 Qt 4.7.4 静态编译的调试与发布 Inchroy's Blog 博客频道 CSDN.NET
    编写可丢弃的代码
    c++ using namespace std; 海明威 博客园
    解决MySQL server has gone away
    nginx upstream 调度策略
    (2006, 'MySQL server has gone away') 错误解决 dba007的空间 51CTO技术博客
    Linux IO模型漫谈(2) 轩脉刃 博客园
    redis源码笔记 initServer 刘浩de技术博客 博客园
    MySQLdb批量插入数据
    词库的扩充百度百科的抓取你知道这些热词吗? rabbit9898 ITeye技术网站
  • 原文地址:https://www.cnblogs.com/liurwei/p/9446284.html
Copyright © 2011-2022 走看看