Create a complete example project django

With virtual environment

Installation and Configuration

Installation command virtual environment:

1) sudo pip install virtualenv # install the virtual environment

2) sudo pip install virtualenvwrapper # install virtual environment Expansion Pack

3) Edit home directory .bashrc file, add the following two lines.

export WORKON_HOME=$HOME/.virtualenvs

source /usr/local/bin/virtualenvwrapper.sh

4) Use source .bashrc about to take effect.

use

Python3 create a virtual environment:

mkvirtualenv -p python3 bj19 # bj19 is the name of a virtual environment

Enter the virtual work environment:

workon virtual environment name

How many virtual machine environment on View:

workon two space + tab key

Exit the virtual environment:

deactivate

To delete a virtual environment:

rmvirtualenv virtual environment name

Virtual environment command to install the package:

pip install the package name

Note: You can not use sudo pip install the package name, this command will install the package to the real host environment rather than installed into a virtual environment.

View virtual environment in which to install python package:

pip list  
pip freeze

Django installation environment:

pip install django==1.8.2

Create a project

Create folder

bj19 mkdir   # bj19 folder name is just starting

Enter bj19 create an application package

manage.py startapp booktest Python   # booktest is the application name, you can easily play

Change the setting settings.py

# Registration application 
the INSTALLED_APPS = (
     ' django.contrib.admin ' ,
     ' django.contrib.auth ' ,
     ' django.contrib.contenttypes ' ,
     ' django.contrib.sessions ' ,
     ' django.contrib.messages ' ,
     ' django.contrib .staticfiles ' ,
     ' booktest ' ,   # booktest registration application was created to create a database without this error will be reported nO changes 
) 
.... 
# template configuration 
tEMPLATES = [ 
    { 
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],  # 指定templates模板目录路径
        '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 ' , 
            ], 
        }, 
    }, 
] 
.... 
# database configuration 
DATABASES = {
     ' default ' : {
         ' ENGINE ' : ' django.db.backends.mysql ' ,   # specify what database 
        ' nAME ' : ' test1 ' ,   # specify the database name 
        ' the USER ' :' The root ' ,   # database account 
        ' PASSWORD ' : ' MySQL ' ,   # database password 
        ' the HOST ' : ' localhost ' ,   # database using native IP localhost 
        ' PORT ' : 3306,   # port 
    } 
} 
.... 
# Localization Configuring 
LANGUAGE_CODE = ' ZH-Hans '   # localized language 

TIME_ZONE = ' Asia / of Shanghai '   # localization time
....
 # static resource allocation 
STATIC_URL = ' / static / ' 
STATICFILES_DIR = [the os.path.join (base_dir, ' static ' )]   # static resource storage path

Create a template folder name to create subdirectories in the corresponding application templates

 

 Create a static resource file package

 

 Create a database test1

create database test1 charset=utf8;

Referenced in the project pymysql

test1 / __ init__.py file

import pymysql
pymysql.install_as_MySQLdb()

To build the table

booktest/models.py中

 

Guess you like

Origin www.cnblogs.com/yifengs/p/11547287.html