• Django 入门


    安装好后进入文件夹会看到里面Django为我们创建好的文件里

    manage.py 为我们启动服务的文件  python manage.py runserver 8080(启动服务的时候默认是8000,后面加8080 可以给他设置启动服务的端口号)

    db.sqlite3 这个文件是为我们提取创建好的连接数据库的文件夹

    进入mysite内

     __init__.py 为初始化文件

    settings.py 是配置文件

    urls.py 是设置路由文件

    wsgi.pt 

    我们进入settings.py看一下相关配置(我在下图已经把相关配置对应的作用标准上备注)

    """
    Django settings for mysite project.
    
    Generated by 'django-admin startproject' using Django 2.0.4.
    
    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

     #imprt os 模块:
     #在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于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 = '3pf%rp#u@h9xu&@utms#kl#=)t(rua=g6h)8lyp$9(7quv9h7!'
    
    # SECURITY WARNING: don't run with debug turned on in production!
    #调试模式
    DEBUG = True
    
    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',
        #备注是的代码为禁止POST请求因为我要用到所以我注释掉了,大家按需求选性即可。
        # 'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        #本地化支持,这是我在原有基础上加,它的作用是判断你本地ip从而帮你翻译Django界面
        'django.middleware.locale.LocaleMiddleware',
    ]
    #定义路由文件
    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
    
    # 数据库配置
    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/'
  • 相关阅读:
    .JDBC访问数据库的基本步骤是什么?
    文本的四种编码方式
    实现不在栈中产生对象
    字符串链接和复制
    堆和栈的区别(详细)
    产生随机数字
    利用返回引用来操作结构体
    inline 内联函数可以避免函数重定义问题
    五大内存分区,堆与栈的区别(转)
    Strategy模式
  • 原文地址:https://www.cnblogs.com/Niuxingyu/p/10281506.html
Copyright © 2020-2023  润新知