博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
弹射起步~django
阅读量:7285 次
发布时间:2019-06-30

本文共 5933 字,大约阅读时间需要 19 分钟。

sudo apt-get installl tree

开始创建工作目录

django-admin.py startproject mysite

创建一个名为 mysite的子目录,然后我们通过tree 命令查看一下目录结构

xpower@xpower-CW65S:~$ tree mysite/mysite/├── manage.py└── mysite    ├── __init__.py    ├── settings.py    ├── urls.py    └── wsgi.py1 directory, 5 files

 


 


mysite : 外面的mysite是项目文件的目录名称罢了,Django不在意他的名字,可以将其改为你喜欢的名字。


 

manage.py :命令行工具集,供我们操作本Django项目,

python3 manage.py help #查看起提供的功能。

注意,千万不要修改该文件。下面我们看一下该文件的内容。

#!/usr/bin/env pythonimport osimport sysif __name__ == "__main__":    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")    try:        from django.core.management import execute_from_command_line    except ImportError:        # The above import may fail for some other reason. Ensure that the        # issue is really that Django is missing to avoid masking other        # exceptions on Python 2.        try:            import django        except ImportError:            raise ImportError(                "Couldn't import Django. Are you sure it's installed and "                "available on your PYTHONPATH environment variable? Did you "                "forget to activate a virtual environment?"            )        raise    execute_from_command_line(sys.argv)
View Code

可以看出其是本项目入口文件。


 

mysite/mysite/ : 这个mysite是我们项目的一个包。


 

__init__.py : 让python吧mysite目录作文一个包的必须文件。一般情况下文件为空。


 

setting.py : 该项目的配置文件,通过该文件,我们可以了解到可以了解到可以进行那些配置和已有的默认值。

"""Django settings for mysite project.Generated by 'django-admin startproject' using Django 1.10.4.For more information on this file, seehttps://docs.djangoproject.com/en/1.10/topics/settings/For the full list of settings and their values, seehttps://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 = '&ey(33(c&&oq)+s%o%*i-xxt=+me2w&c-ka50%s)urmxxahb!h'# SECURITY WARNING: don't run with debug turned on in production!DEBUG = TrueALLOWED_HOSTS = []# Application definitionINSTALLED_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',]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/1.10/ref/settings/#databasesDATABASES = {    '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-validatorsAUTH_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 = TrueUSE_L10N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)# https://docs.djangoproject.com/en/1.10/howto/static-files/STATIC_URL = '/static/'
View Code

项目中需要的配置信息都在里面,包括运行结构,和网站部署等等。

urls.py : Django的URL设置,网站目录。

wsgi.py : 是兼容WSGI的Web服务器伺服你的项目的入口文件。更多细节请查阅“如何通过WSGI部署”(



下面我们看一下 manage.py可以提供的服务。

xpower@xpower-CW65S:~/mysite$ python3 manage.py helpType 'manage.py help 
' 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
View Code

通过以上信息,我们开始运行我们的开发服务器。

python3 manage.py runserver

得到如下信息表示成功启动

xpower@xpower-CW65S:~/mysite$ python3 manage.py runserverPerforming 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.February 13, 2017 - 19:08:59Django version 1.10.4, using settings 'mysite.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.

打开链接可以看到 It‘s worked !界面。

Django自带的这个开发服务器虽然方便,但是它只能在同一时间可靠地处理一个链接,而且没有任何安全检查,在发布我们的站点之前,我们还需要了解如何部署Django。


默认情况下runserver的时候我们启动的是8000端口,上述反馈信息中得出。如果端口占用或者我们需要指定端口的时候使用

python manage.py runserver 8080

 

转载于:https://www.cnblogs.com/A-FM/p/6396050.html

你可能感兴趣的文章
机器学习理论篇1:机器学习的数学基础
查看>>
Python 守护进程
查看>>
七牛云王珂 直播分享 | 如何快速搭建智能化的统一日志管理系统
查看>>
BusyBox
查看>>
configure make make install in linux
查看>>
剑指offer:调整数组顺序使奇数位于偶数前面
查看>>
一步一步学Silverlight 2系列(3):界面布局
查看>>
本人部分博客导航(ing...)
查看>>
redis环境搭建笔记
查看>>
作业一:计算机是如何工作的进行
查看>>
使用Spring JdbcTemplate实现数据库操作
查看>>
上传文件前台后台必备的条件
查看>>
内存管理原则
查看>>
C++运算符重载详解
查看>>
Analyzing Storage Performance using the Windows Performance Analysis ToolKit (WPT)
查看>>
Android测试之Monkey
查看>>
Android实现二维码扫描登录网页
查看>>
关于一些Python的一些基础语法训练
查看>>
性能提升永远在路上 全闪存如何求突破?
查看>>
苹果计划在美组装计算机:面向数据中心
查看>>