美文网首页
Django学习(一)

Django学习(一)

作者: 测试游记 | 来源:发表于2019-05-26 15:40 被阅读0次

    欢迎关注我的公众号:zx94_11

    环境搭建

    安装环境

    $ pip install virtualenv
    $ virtualenv my_env
    $ source my_env/bin/activate
    $ pip install Django==2.0.5
    
    image-20190526124442888

    校验环境

    >>> import django
    >>> django.get_version()
    '2.0.5'
    
    校验环境

    创建项目

    $ django-admin startproject mysite
    

    数据库初始化

    $ python manage.py migrate
    
    数据库初始化

    运行Django

    $ python manage.py runserver
    
    运行

    结果查看

    访问http://127.0.0.1:8000/查看运行结果

    image-20190526125508550

    了解配置文件

    需要了解部分添加了中文注释,其他部分之后再学习

    """
    Django settings for mysite project.
    
    Generated by 'django-admin startproject' using Django 2.0.5.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/2.0/topics/settings/
    
    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/2.0/ref/settings/
    """
    
    import os
    
    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
    
    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = '6#8iyxsrt!ssfw9&)@v-vtmos+tl+mc+!)!aib36160^5326mu'
    
    # SECURITY WARNING: don't run with debug turned on in production!
    # 调试模式
    # 开启/禁用当前项目的调试模式
    # 设置为True:当程序抛出未捕获的异常时,Django会显示详细的错误页面。
    DEBUG = True
    
    # 当开启调试模式或执行测试的时候,不可用
    # 当DEBUG设置为False的时候,需要将域/主机添加到此项,以允许它为Django站点服务。
    ALLOWED_HOSTS = []
    
    # Application definition
    # 通知Django针对当前站点的哪一个应用程序处于活动状态
    INSTALLED_APPS = [
        'django.contrib.admin',  # 管理站点
        'django.contrib.auth',  # 验证框架
        'django.contrib.contenttypes',  # 处理内容类型的框
        'django.contrib.sessions',  # 会话框架
        'django.contrib.messages',  # 消息机制框架
        'django.contrib.staticfiles',  # 管理静态文件的框架
    ]
    
    # 中间件列表
    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',
    ]
    
    # 定义了程序根URL路径
    ROOT_URLCONF = 'mysite.urls'
    
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            '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 = 'mysite.wsgi.application'
    
    # Database
    # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
    # 数据库设置,默认使用SQLite3
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }
    
    # Password validation
    # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
    
    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',
        },
    ]
    
    # Internationalization
    # https://docs.djangoproject.com/en/2.0/topics/i18n/
    
    LANGUAGE_CODE = 'en-us'
    
    TIME_ZONE = 'UTC'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = True  # 启用/禁用时区支持
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/2.0/howto/static-files/
    
    STATIC_URL = '/static/'
    
    

    相关文章

      网友评论

          本文标题:Django学习(一)

          本文链接:https://www.haomeiwen.com/subject/phfftctx.html