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 !

  • 相关阅读:
    如何理解volatile关键字
    Spring 4+JMS+ActiveMQ Example with Annotations
    Spring MVC如何获得客户端IP地址
    PDFBox –如何读取PDF的内容
    Spring MVC 使用Ehcache作为缓存的例子
    自定义了一个data table控件
    Linux基本配置
    位操作学习
    QT——QPainter类详解
    QT——设计文件的和控制类的关联问题
  • 原文地址:https://www.cnblogs.com/liurwei/p/9446284.html
Copyright © 2011-2022 走看看