创建Django项目

一 创建项目

1 进入命令行,进入想要安装项目的目录

2 命令行输入:django-admin startproject myblog

3 若没有报错,则项目成功

举例:

E:\Django>django-admin startproject myblog

E:\Django>dir
驱动器 E 中的卷是 Study
卷的序列号是 E6E8-2BFB

E:\Django 的目录

2018/11/28  19:15    <DIR>          .
2018/11/28  19:15    <DIR>          ..
2018/11/28  19:15    <DIR>          myblog
               0 个文件              0 字节
               3 个目录 43,522,568,192 可用字节
二 项目结构

二 项目结构

1 manage.py

与项目进行交互的命令行工具集的入口,就是项目管理器。

执行python manage.py 来查看所有命令

E:\Django\myblog>python manage.py

Type 'manage.py help <subcommand>' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver

1.1 启动服务命令

E:\Django\myblog>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 28, 2018 - 19:22:17
Django version 1.10.2, using settings 'myblog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

打开浏览器

1.2 启动特定端口

E:\Django\myblog>python manage.py runserver 9999
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 28, 2018 - 19:26:03
Django version 1.10.2, using settings 'myblog.settings'
Starting development server at http://127.0.0.1:9999/
Quit the server with CTRL-BREAK.

打开浏览器

2 myblog目录

项目的一个容器

包含项目最基本的一些配置

目录名称不建议修改

3 wsgi.py

WSGI(Python Web Server Gateway Interface)

中文名:Python服务器网关接口

Python应用与Web服务器之间的接口

4 urls.py

URL配置文件

Django项目中所有地址(页面)都需要我们去配置其URL

5 settings.py

项目的总配置文件

里面包含了数据库,Web应用、时间等各种配置

5.1 配置测试

DEBUG = False
ALLOWED_HOSTS = ['localhost']

访问页面:

5.2 配置测试

DEBUG = True
ALLOWED_HOSTS = ['localhost']

访问页面

5.3 配置说明

# -*- coding: utf-8 -*-
"""
Django settings for myblog project.

Generated by 'django-admin startproject' using Django 1.10.2.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/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/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 's=uuksb7fbrjsx1)(u#v1=y1($po&e%^f073&3mbq1f5zcu410'

# SECURITY WARNING: don't run with debug turned on in production!
# 调试,实际生产用False
DEBUG = True

# 当DEBUG配置为False,Django只允许下面配置的主机访问网站
ALLOWED_HOSTS = []


# Application definition
# 已安装的应用,下面是创建项目时自带的,我们创建的应用要加入该列表
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 = 'myblog.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 = 'myblog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/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/1.10/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/1.10/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/1.10/howto/static-files/
# 静态文件地址
STATIC_URL = '/static/'

8 __init__.py

Python中声明模块的文件

内容默认为空

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值