彈射起步~django

sudo apt-get installl tree

開始建立工做目錄python

django-admin.py startproject mysite

建立一個名爲 mysite的子目錄,而後咱們經過tree 命令查看一下目錄結構sql

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

1 directory, 5 files

 


 


mysite : 外面的mysite是項目文件的目錄名稱罷了,Django不在乎他的名字,能夠將其改成你喜歡的名字。shell


 

manage.py :命令行工具集,供咱們操做本Django項目,django

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

注意,千萬不要修改該文件。下面咱們看一下該文件的內容。安全

#!/usr/bin/env python
import os
import sys

if __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是咱們項目的一個包。session


 

__init__.py : 讓python吧mysite目錄做文一個包的必須文件。通常狀況下文件爲空。app


 

setting.py : 該項目的配置文件,經過該文件,咱們能夠了解到能夠了解到能夠進行那些配置和已有的默認值。ide

"""
Django settings for mysite project.

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

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 = '&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 = True

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',
]

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/#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/'
View Code

項目中須要的配置信息都在裏面,包括運行結構,和網站部署等等。工具

urls.py : Django的URL設置,網站目錄。

wsgi.py : 是兼容WSGI的Web服務器伺服你的項目的入口文件。更多細節請查閱「如何經過WSGI部署」(https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/)。



下面咱們看一下 manage.py能夠提供的服務。

xpower@xpower-CW65S:~/mysite$ python3 manage.py help

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
View Code

經過以上信息,咱們開始運行咱們的開發服務器。

python3 manage.py runserver

獲得以下信息表示成功啓動

xpower@xpower-CW65S:~/mysite$ python3 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.

February 13, 2017 - 19:08:59
Django 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
相關文章
相關標籤/搜索