Recently in learning Python, today we have found that Django is such a powerful web framework that I have to try.
1. Install Python, the official website recommends using Python3:
So, download python-3.4.1.tgz, unzip, install:
./configure
./make
./make Install
Of course, when the installation may require GCC, or configure check the past, can be used yum-y install gcc simple rough installation, after both check and install python3.4. Next, specify the path to the default python: There are Python, Python2, python2.7 three files in/usr/bin, which in turn point to the latter, and we'll back up the python (sudo mv python python.bak), then create the Python3 soft link (sudo ln-s/usr/local/python3/bin/python3/usr/bin/python). This will replace the default Python version with Python3.
Because Yum uses python2, it may not work properly after replacing with Python3, so modify the Yum configuration file (sudo vi/usr/bin/yum). change the python version specified in the first line to python2.7 (#!/usr/bin/python to #!/usr/bin/python2.7)
OK, Python 3.4 is ready to install!
2. Install PIP:
Since many Python-related software is easy to download with Pip, we download and install the PIP tool:
wget https://bootstrap.pypa.io/get-pip.py
Python get-pip.py
3. Django Installation:
OK, after you support the PIP command, install Django directly.
Of course, my Django is already from the official website, of course you can also download: wget https://www.djangoproject.com/download/1.10/tarball/
This is the latest version 1.10 Oh!
pip install Django-1.10, after installation:
4. OK, Next, create the first project with Django
Online some, simple rude (specific command meaning can be Baidu):
Django-admin. PY startproject HelloWorld
The directory structure after creation
Catalogue Description:
- HelloWorld: the container for the project.
- manage.py: a useful command-line tool that allows you to interact with the Django project in a variety of ways.
- helloworld/__init__.py: An empty file that tells Python that the directory is a python package.
- helloworld/settings.py: setup/configuration for this Django project.
- helloworld/urls.py: URL declaration for the Django project; A "directory" of Django-driven Web sites.
- helloworld/wsgi.py: a WSGI-compatible WEB server portal to run your project
The next step is to start the Web service, but configure the database, or you will not be able to find the SQLite3 (because it is the default)
Find the default database configuration in settings.py and change it to the MySQL database I already have.
Specific configuration can refer to the official website document address also help you comment out: https://docs.djangoproject.com/en/1.10/ref/settings/#databases
At this point, start with any port:python manage. PY runserver 0.0. 0.0:8000
(0.0.0.0 lets other computers connect to the development server, 8000 is the port number.) If not, the port number defaults to 8000)
There is a good chance of error: Error loading mysqldb module:no module named ' MySQLdb '
At this time because Python3.4 does not support MYSQLDB, struggling to find a witty young man told me:
Alternatives-pymysql
Pymysql usage is exactly the same as MYSQLDB, just remember to add the following code to the _init__. py file in the site:
import pymysql pymysql.install_as_MySQLdb()
之后发现居然木有pymysql,不过此时安装一下即可:pip install pymysql 或者 yum pymsql
至此再次启动Web服务,python manage.py runserver 0.0.0.0:8000 &
界面(要注意防火墙打开所需的端口,如此处的8000):
至此,就可以开始在Django的Web服务上面随心所欲的编写自己的APP啦!
CentOS 7 Deployment Django to run up the first Web service