Django creation and use of the overall process

The previous blogs have introduced the detailed use procedures in Django. Our commands are all integrated and the data is added for testing.

Project preparation

  1. Create project

    django-admin startproject bookmanager
    
  2. Create application

    python manager.py startapp book
    
  3. Replace python interpreter: select as needed

    # 进入指定虚拟环境
    which python
    
    # python2
    /home/python/.virtualenvs/py_django/bin/python
    
    # python3
    /home/python/.virtualenvs/py3_django/bin/python
    
  4. Install the app

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        #添加子应用
        'book.apps.BookConfig'
    ]
    
  5. Localization

    #设置中文
    LANGUAGE_CODE = 'zh-Hans'
    #亚洲上海时区
    TIME_ZONE = 'Asia/Shanghai'
    
  6. Template path

    在应用同级目录下,创建templates 模板文件夹

    Then create the path

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR,'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',
                ],
            },
        },
    ]
    
  7. Match urls in the project

    Regular: As long as the path is not admin /, the match is successful. And include urls.py in the app

    from django.conf.urls import url,include
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        #正则为:只要不是 admin/ 就算匹配成功
        url(r'^',include('book.urls'))
    ]
    
  8. Match urls.py in the application

    Create urls.py in the application

    Regular: the path contains booklist / , then call the corresponding bookList function in the view

    from django.conf.urls import url
    from book.views import bookList
    
    urlpatterns = [
    
        # 匹配书籍列表信息的URL,调用对应的bookList视图
        url(r'^booklist/$',bookList)
    ]
    
  9. Prepare view

    # 定义视图:提供书籍列表信息
    def bookList(request):
    
        return HttpResponse('OK!')
    
  10. Turn on the server, test the project

    # 进入项目文件中, 开启项目对应的服务器
    python manage.py runserver
    
    # 浏览器中输入网址
    http://127.0.0.1:8000/booklist/
    

Configuration

The connection configuration information of the database is saved in settings.py, and the initial configuration of Django uses the sqlite database by default.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
  1. To use the MySQL database, you need to install the driver first

    使用MySQL数据库首先需要安装驱动程序
    
  2. Add the following statement to the __init__.py file in the subdirectory of the Django project with the same name

    import pymysql
    
    pymysql.install_as_MySQLdb()
    

    The role is to allow Django's ORM to call PyMySQL with mysqldb.

  3. Modify DATABASES configuration information

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '127.0.0.1',  # 数据库主机
            'PORT': 3306,  # 数据库端口
            'USER': 'root',  # 数据库用户名
            'PASSWORD': 'mysql',  # 数据库用户密码
            'NAME': 'book'  # 数据库名字
        }
    }
    
  4. Create a database in MySQL

    create database book charset=utf8;
    

Then it is to define the model, so the created usage flow is OK, if you want to see the next model class, please see the next article.

Published 125 original articles · Like 260 · Visits 120,000+

Guess you like

Origin blog.csdn.net/weixin_44685869/article/details/105362452