python之路-----web应用的创建(python3.4下,django连接mysql)
2015-08-19 14:54
1026 查看
参考网址:
https://github.com/PyMySQL/PyMySQL/
django框架中的一个网站可以包含多个django项目,而一个django项目则包含一组特定的对象,这些对象包括URL的设计、数据库的设计以及其他的一些选项设置。
django-admin.py-----它有许多的命令选项,可以通过这些选项来操作项目。其中使用startproject命令选项,可以生成一个项目名的目录,它包括一个基本web应用所需要的文件结构:
__init__.py:空文件,主要用来告诉python将此网站目录当做是python的包
manage.py:可以使网站管理员来管理django项目
settings.py:django项目的配置文件
urls.py:编写包括URL的配置文件,它是用户访问django应用的方式
注意:
(1)django的项目是作为python的包来处理的,所以项目命名的时候尽量不要和已有的python模块中的名字冲突。
1、新建一个项目和一个app
注意:
(1)执行python3.4 manage.py startapp firstapp时:ImportError: No module named 'MySQLdb'
Python标准库中并没有集成MySQL接口程序,MySQLdb是一个第三方包,需独立下载并安装。目前MySQLdb并不支持python3.x,可以安装 PyMySQL代替mysql-python。
# yum -y install MySQL-python.x86_64 ##安装后,必须重新编译python,但是使用默认python。
For Connector/Python 2.0, the source tree has been reorganized to have a single code base, for easier maintenance, testing, and distribution.
# tar xvf mysql-connector-python-2.0.4.tar.gz
# python3.4 setup.py install --help
# python3.4 setup.py install
copying build/lib/mysql/connector/catch23.py -> /usr/local/python3.4.3/lib/python3.4/site-packages/mysql/connector
(2)manage.py创建一个app时,它会读settings.py的配置
(3)pymysql.err.InternalError: (1130, "Host '::1' is not allowed to connect to this MySQL server")
DATABASES的设置PASSWORD等选项必须是大写的
# mkdir -p /www/django/
# python3.4 /usr/local/python3.4.3/bin/django-admin.py startproject firstproject /www/django/ ##创建firstproject的目录
#The last stable release is available on PyPI and can be installed with pip
# /usr/local/python3.4.3/bin/pip3.4 install PyMySQL
Downloading PyMySQL-0.6.6-py2.py3-none-any.whl
# vim firstproject/__init__.py
import pymysql
pymysql.install_as_MySQLdb()
# pwd
/www/django/firstproject
# vim settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', ##数据库引擎
'NAME': 'django', ##数据库名
'USER': 'yangsq',
'PASSWORD': '123456',
'HOST': '192.168.0.3',
'PORT': '3306',
}
}
# python3.4 manage.py startapp firstapp
# python3.4 manage.py syncdb ##创建配置文件INSTALLED_APPS域中还没有创建的表
2、django框架中的轻量级web应用服务器
django内置的服务器可以在代码修改的时候自动加载,从而实现网站的迅速开发。
# python3.4 manage.py runserver 0.0.0.0:8000 & ##使用http://192.168.0.4:8000/访问网页
3、使用模板显示页面
3.1、把项目加入到settings.INSTALLED_APPS
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'firstapp' ##添加项目,像导入模块
)
3.2、在firstapp/views.py中编写一个视图
from django.shortcuts import render
def call_home(request):
return render(request, 'home.html')
# mkdir -p templates
# cat home.html
<!DOCTYPE html>
<html>
<head>
<title>hello world</title>
</head>
<body>
python say:hello world
</body>
</html>
3.4、将视图中的函数映射到网址(修改 项目名/urls.py)
# vim urls.py
url(r'^$','firstapp.views.call_home',name='home'),
3.5、验证home.html
访问http://192.168.0.4:8000/,得到python say:hello world
4、views返回json对象到模板
1、修改views.py添加一个list变量
# cat views.py
#coding=utf8
from __future__ import unicode_literals
from django.shortcuts import render
import json
def call_home(request):
list=['中国人','热爱','china']
return render(request, 'home.html',{'return_list':json.dumps(list)}
2、修改模板文件home.html
注意
(1)必须加一个safe过滤器,否则就报错:Unexpected token &
<!DOCTYPE html>
<html>
<head>
<title>hello world</title>
</head>
<body>
<p id="p1"> python say:hello world </p>
<script>
var list={{ return_list|safe }}
alert(list);
document.getElementById("p1").innerHTML="hi,python";
</script>
</body>
</html>
https://github.com/PyMySQL/PyMySQL/
django框架中的一个网站可以包含多个django项目,而一个django项目则包含一组特定的对象,这些对象包括URL的设计、数据库的设计以及其他的一些选项设置。
django-admin.py-----它有许多的命令选项,可以通过这些选项来操作项目。其中使用startproject命令选项,可以生成一个项目名的目录,它包括一个基本web应用所需要的文件结构:
__init__.py:空文件,主要用来告诉python将此网站目录当做是python的包
manage.py:可以使网站管理员来管理django项目
settings.py:django项目的配置文件
urls.py:编写包括URL的配置文件,它是用户访问django应用的方式
注意:
(1)django的项目是作为python的包来处理的,所以项目命名的时候尽量不要和已有的python模块中的名字冲突。
1、新建一个项目和一个app
注意:
(1)执行python3.4 manage.py startapp firstapp时:ImportError: No module named 'MySQLdb'
Python标准库中并没有集成MySQL接口程序,MySQLdb是一个第三方包,需独立下载并安装。目前MySQLdb并不支持python3.x,可以安装 PyMySQL代替mysql-python。
# yum -y install MySQL-python.x86_64 ##安装后,必须重新编译python,但是使用默认python。
For Connector/Python 2.0, the source tree has been reorganized to have a single code base, for easier maintenance, testing, and distribution.
# tar xvf mysql-connector-python-2.0.4.tar.gz
# python3.4 setup.py install --help
# python3.4 setup.py install
copying build/lib/mysql/connector/catch23.py -> /usr/local/python3.4.3/lib/python3.4/site-packages/mysql/connector
(2)manage.py创建一个app时,它会读settings.py的配置
(3)pymysql.err.InternalError: (1130, "Host '::1' is not allowed to connect to this MySQL server")
DATABASES的设置PASSWORD等选项必须是大写的
# mkdir -p /www/django/
# python3.4 /usr/local/python3.4.3/bin/django-admin.py startproject firstproject /www/django/ ##创建firstproject的目录
#The last stable release is available on PyPI and can be installed with pip
# /usr/local/python3.4.3/bin/pip3.4 install PyMySQL
Downloading PyMySQL-0.6.6-py2.py3-none-any.whl
# vim firstproject/__init__.py
import pymysql
pymysql.install_as_MySQLdb()
# pwd
/www/django/firstproject
# vim settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', ##数据库引擎
'NAME': 'django', ##数据库名
'USER': 'yangsq',
'PASSWORD': '123456',
'HOST': '192.168.0.3',
'PORT': '3306',
}
}
# python3.4 manage.py startapp firstapp
# python3.4 manage.py syncdb ##创建配置文件INSTALLED_APPS域中还没有创建的表
2、django框架中的轻量级web应用服务器
django内置的服务器可以在代码修改的时候自动加载,从而实现网站的迅速开发。
# python3.4 manage.py runserver 0.0.0.0:8000 & ##使用http://192.168.0.4:8000/访问网页
3、使用模板显示页面
3.1、把项目加入到settings.INSTALLED_APPS
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'firstapp' ##添加项目,像导入模块
)
3.2、在firstapp/views.py中编写一个视图
from django.shortcuts import render
def call_home(request):
return render(request, 'home.html')
# mkdir -p templates
# cat home.html
<!DOCTYPE html>
<html>
<head>
<title>hello world</title>
</head>
<body>
python say:hello world
</body>
</html>
3.4、将视图中的函数映射到网址(修改 项目名/urls.py)
# vim urls.py
url(r'^$','firstapp.views.call_home',name='home'),
3.5、验证home.html
访问http://192.168.0.4:8000/,得到python say:hello world
4、views返回json对象到模板
1、修改views.py添加一个list变量
# cat views.py
#coding=utf8
from __future__ import unicode_literals
from django.shortcuts import render
import json
def call_home(request):
list=['中国人','热爱','china']
return render(request, 'home.html',{'return_list':json.dumps(list)}
2、修改模板文件home.html
注意
(1)必须加一个safe过滤器,否则就报错:Unexpected token &
<!DOCTYPE html>
<html>
<head>
<title>hello world</title>
</head>
<body>
<p id="p1"> python say:hello world </p>
<script>
var list={{ return_list|safe }}
alert(list);
document.getElementById("p1").innerHTML="hi,python";
</script>
</body>
</html>
相关文章推荐
- Python中如何创建文件夹和嵌套文件夹的方法
- Python——面向对象
- 03-树2. List Leaves (25) Python C
- wxPython+Boa Constructor环境配置
- Python数组排序
- wxpython 基本的控件 (文本)
- wxPython中文教程入门实例
- 如何选择python web开发框架
- python-闭包
- 详解python2 和 python3的区别
- python图像处理:pytesseract和PIL
- suds 在python3.x上的安装并访问webservice
- KMP算法的python实现
- 使用Python将csv文件批量化导入SQL Server
- python 文件操作
- Python中集合的内建函数和内建方法学习教程
- python ML 笔记:Kmeans
- 深入解析Python中的集合类型操作符
- 【对象序列化】pickle.dumps()--python对象序列化/反序列化
- 【Python】[面性对象编程] 获取对象信息,实例属性和类属性