Django from getting started to giving up 001. Preliminary study

Source: Internet
Author: User
Tags install django virtualenv

Environment : To enable the server to develop multiple applications, the environment version does not affect each other, using virtualenv to create isolated isolated environments for development use.

#安装virtualenv:

[Email protected] django]# PIP3 install virtualenv

#创建一个虚拟环境

[Email protected] django]# virtualenv django_env

Using base prefix '/usr/local '

New Python executable in/django/django_env/bin/python3.5

Also Creating executable In/django/django_env/bin/python

Installing Setuptools, Pip, Wheel...done.

#使用虚拟环境

[Email protected] django]# source Django_env/bin/activate

#在虚拟环境下安装django

(django_env) [Email protected] django]# PIP3 install django==1.10

Terms:

The role of project is to provide configuration files, such as where to define database connection information, installed app lists, Template_dirs, and so on.

An app is a set of Django features, often including models and views, in the way Python's package structure exists.

For example, Django itself has some apps, such as a comment system and an automated management interface. One key point of the app is that they are easily ported to other project and reused by multiple project.

#1. Create a project

(django_env) [Email protected] django]# django-admin startproject my_project

After you create a project, several files are generated

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/8C/A6/wKioL1hzciLQetdQAAAQ6d63izA016.png-wh_500x0-wm_ 3-wmp_4-s_1333965089.png "title=" 1.png "alt=" Wkiol1hzcilqetdqaaaq6d63iza016.png-wh_50 "/>

#2. Create App app, here are two ways to

(django_env) [Email protected] my_project]# django-admin Startapp First_app

(django_env) [Email protected] my_project]# Python3 manage.py Startapp Second_app

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8C/A6/wKioL1hzcjeCpWqoAAAk1Zhhys4132.png-wh_500x0-wm_ 3-wmp_4-s_225434209.png "title=" 2.png "alt=" Wkiol1hzcjecpwqoaaak1zhhys4132.png-wh_50 "/>

#3. Connect to the database, use Pymsql to connect mysqldb in Python3, or you will encounter the pit below

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8C/AA/wKiom1hzcknzjQXBAAAZVtegWRU698.png-wh_500x0-wm_ 3-wmp_4-s_3062666166.png "title=" 3.png "alt=" Wkiom1hzcknzjqxbaaazvtegwru698.png-wh_50 "/>

Workaround:

1). Install the Pymysql wheel:

(django_env) [[email protected] my_project]# PIP3 install Pymysql

2). After successful execution, open __init__.py and add the following:

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8C/A6/wKioL1hzclfwg4p3AAAND1ZnIf0424.png-wh_500x0-wm_ 3-wmp_4-s_1355569349.png "title=" 4.png "alt=" Wkiol1hzclfwg4p3aaand1znif0424.png-wh_50 "/>

3). Modify settings.py:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8C/A6/wKioL1hzcmrxA037AAAW4j9Vv30261.png-wh_500x0-wm_ 3-wmp_4-s_1078726292.png "title=" 5.png "alt=" Wkiol1hzcmrxa037aaaw4j9vv30261.png-wh_50 "/>

4). Synchronize the database:

This method creates a table, and when you add a class to the models.py, you can run it to automatically create a table in the database without creating it manually.

(django_env) [[email protected] my_project]#python3 manage.py Migrate

After the database synchronization succeeds, some tables are generated in the connected library.

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8C/AA/wKiom1hzcnvAW3cKAAAamTfK2g8250.png-wh_500x0-wm_ 3-wmp_4-s_3258329732.png "title=" 6d.png "alt=" Wkiom1hzcnvaw3ckaaaamtfk2g8250.png-wh_50 "/>

#4. Use your own development server and use it only for debugging at development time, not recommended online

(django_env) [Email protected] my_project]# Python3 manage.py runserver 0.0.0.0:8000

Performing system checks ...

System Check identified no issues (0 silenced).

January 04, 2017-02:24:16

Django version 1.10, using Settings ' my_project.settings '

Starting development Server at Http://0.0.0.0:8000/

Quit the server with Control-c.

[04/jan/2017 02:25:15] "get/http/1.1" 200 1767

Not Found:/favicon.ico

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8C/A6/wKioL1hzco6yUYKmAABfb1P7B6I820.png-wh_500x0-wm_ 3-wmp_4-s_4171212508.png "title=" 7.png "alt=" Wkiol1hzco6yuykmaabfb1p7b6i820.png-wh_50 "/>

#创建后台登录账号密码

(django_env) [Email protected] my_project]# Python3 manage.py createsuperuser

Username (leave blank to use ' root '): admin

Email address: [Email protected]

Password:

Password (again):

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8C/A6/wKioL1hzcqLD32OmAABS2JWjk8U257.png-wh_500x0-wm_ 3-wmp_4-s_3545934667.png "title=" 8.png "alt=" Wkiol1hzcqld32omaabs2jwjk8u257.png-wh_50 "/>

The true face of Lushan



This article is from the "unplug the Operational Space" blog, please be sure to keep this source http://zhangdj.blog.51cto.com/9210512/1890535

Django from getting started to giving up 001. Preliminary study

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.