python之路-----web应用的创建(python3.4下,django连接mysql)

参考网址:
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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值