zoukankan      html  css  js  c++  java
  • Django学习笔记 Django的工程目录

    mysite
    ├── manage.py 管理项目:包括数据库建立、服务器运行、测试……
    └── mysite
        ├── __init__.py
        ├── settings.py 配置文件:应用、中间件、数据库、静态目录各种配置……
        ├── urls.py URL映射配置文件L决定一个url访问被哪个程序(函数)响应
        └── wsgi.py Python应用程序或框架和Web服务器之间接口
    运行python manage.py可以看到其后可以跟的一些命令:

    [auth]
        changepassword 更改超级用户的密码
        createsuperuser 创建一个超级用户(用于管理django后台)
    
    [django]
        check
        compilemessages
        createcachetable
        dbshell
        diffsettings
        dumpdata
        flush
        inspectdb
        loaddata
        makemessages
        makemigrations
        migrate 用于创建数据库的
        sendtestemail
        shell 给我们提供一个用于调试的shell
        showmigrations
        sqlflush
        sqlmigrate
        sqlsequencereset
        squashmigrations
        startapp
        startproject
        test
        testserver
    
    [sessions]
        clearsessions
    
    [staticfiles]
        collectstatic
        findstatic
        runserver
    python manage.py后面带的参数

    通过命令:python manage.py runserver 启动一个开发服务器。
    通过命令:python manage.py runserver 0.0.0.0:8080 更改服务器启动的host和端口(变为http://0.0.0.0:8080)。
    通过命令:python manage.py shell 可以进入django提供给我们的一个交互式命令行。在该交互式命令行中可以进行很多操作,如直接访问数据库。

      1 """
      2 Django settings for mysite project.
      3 
      4 Generated by 'django-admin startproject' using Django 1.9.5.
      5 
      6 For more information on this file, see
      7 https://docs.djangoproject.com/en/1.9/topics/settings/
      8 
      9 For the full list of settings and their values, see
     10 https://docs.djangoproject.com/en/1.9/ref/settings/
     11 """
     12 
     13 import os
     14 
     15 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
     16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     17 
     18 
     19 # Quick-start development settings - unsuitable for production
     20 # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
     21 
     22 # SECURITY WARNING: keep the secret key used in production secret!
     23 SECRET_KEY = 'uva%@my)iwu(qhj98()oki*xe@k+s-difty&248rym9*b^f20-'
     24 
     25 # SECURITY WARNING: don't run with debug turned on in production!
     26 DEBUG = True
     27 
     28 ALLOWED_HOSTS = []
     29 
     30 
     31 # Application definition
     32 
     33 INSTALLED_APPS = [
     34     'django.contrib.admin',
     35     'django.contrib.auth',
     36     'django.contrib.contenttypes',
     37     'django.contrib.sessions',
     38     'django.contrib.messages',
     39     'django.contrib.staticfiles',
     40 ]
     41 
     42 MIDDLEWARE_CLASSES = [
     43     'django.middleware.security.SecurityMiddleware',
     44     'django.contrib.sessions.middleware.SessionMiddleware',
     45     'django.middleware.common.CommonMiddleware',
     46     'django.middleware.csrf.CsrfViewMiddleware',
     47     'django.contrib.auth.middleware.AuthenticationMiddleware',
     48     'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
     49     'django.contrib.messages.middleware.MessageMiddleware',
     50     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     51 ]
     52 
     53 ROOT_URLCONF = 'mysite.urls'
     54 
     55 TEMPLATES = [
     56     {
     57         'BACKEND': 'django.template.backends.django.DjangoTemplates',
     58         'DIRS': [],
     59         'APP_DIRS': True,
     60         'OPTIONS': {
     61             'context_processors': [
     62                 'django.template.context_processors.debug',
     63                 'django.template.context_processors.request',
     64                 'django.contrib.auth.context_processors.auth',
     65                 'django.contrib.messages.context_processors.messages',
     66             ],
     67         },
     68     },
     69 ]
     70 
     71 WSGI_APPLICATION = 'mysite.wsgi.application'
     72 
     73 
     74 # Database
     75 # https://docs.djangoproject.com/en/1.9/ref/settings/#databases
     76 
     77 DATABASES = {
     78     'default': {
     79         'ENGINE': 'django.db.backends.sqlite3',
     80         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
     81     }
     82 }
     83 
     84 
     85 # Password validation
     86 # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
     87 
     88 AUTH_PASSWORD_VALIDATORS = [
     89     {
     90         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
     91     },
     92     {
     93         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
     94     },
     95     {
     96         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
     97     },
     98     {
     99         'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    100     },
    101 ]
    102 
    103 
    104 # Internationalization
    105 # https://docs.djangoproject.com/en/1.9/topics/i18n/
    106 
    107 LANGUAGE_CODE = 'en-us'
    108 
    109 TIME_ZONE = 'UTC'
    110 
    111 USE_I18N = True
    112 
    113 USE_L10N = True
    114 
    115 USE_TZ = True
    116 
    117 
    118 # Static files (CSS, JavaScript, Images)
    119 # https://docs.djangoproject.com/en/1.9/howto/static-files/
    120 
    121 STATIC_URL = '/static/'
    settings.py

     整个网站的所有配置相关信息都在setting.py中。
    ALLOWED_HOST : 允许访问的IP域
    INSTALLED_APPS : 已经安装的引用
    MIDDLEWARE_CLASSES : 已经安装的中间件
    ROOT_URLCONF = 'mysite.urls' URL的根目录
    TEMPLATES : 模板引擎
    DATABASES : 数据库引擎
    LANGUAGE_CODE : 网站的语言
    TIME_ZONE : 时区

     1 """mysite URL Configuration
     2 
     3 The `urlpatterns` list routes URLs to views. For more information please see:
     4     https://docs.djangoproject.com/en/1.9/topics/http/urls/
     5 Examples:
     6 Function views
     7     1. Add an import:  from my_app import views
     8     2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
     9 Class-based views
    10     1. Add an import:  from other_app.views import Home
    11     2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
    12 Including another URLconf
    13     1. Import the include() function: from django.conf.urls import url, include
    14     2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
    15 """
    16 from django.conf.urls import url
    17 from django.contrib import admin
    18 
    19 urlpatterns = [
    20     url(r'^admin/', admin.site.urls),
    21 ]
    urls.py

    urlpatterns表中有关于映射的所有信息。

     1 """
     2 WSGI config for mysite project.
     3 
     4 It exposes the WSGI callable as a module-level variable named ``application``.
     5 
     6 For more information on this file, see
     7 https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
     8 """
     9 
    10 import os
    11 
    12 from django.core.wsgi import get_wsgi_application
    13 
    14 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
    15 
    16 application = get_wsgi_application()
    wsgi.py

    django起的是一个轻量级的调试服务器,真正做到产品的时候是用的大型的服务器。
    django和大型服务器之间的接口是通过wsgi来配置的。

  • 相关阅读:
    x86 hook跳转内存地址转换计算框架
    win10180317134配合VS2017搭建WDK驱动开发环境
    C/C++字节特征码转换自动格式化文本工具算法源码
    jar包直接拷贝到WEBINF/lib下和以userLibrary引入的区别
    java.io.IOException: Cannot rename original file to %TOMCAT_HOME%\conf\tomcatusers.xml.old
    Tomcat5 和 Tomcat6 类加载器架构
    Tomcat version * only supports J2EE * Web modules
    tomcat启动时报错:org.apache.catalina.core.AprLifecycleListener init
    tomcat 不能正常启动,双击 startup.bat 一闪而过
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5365293.html
Copyright © 2011-2022 走看看