1、Redis

(1)启动Redis 服务端客户端命令

服务端

ps aux | grep redis 查看redis服务器进程
sudo kill -9 pid 杀死redis服务器
sudo redis-server /etc/redis/redis.conf 指定加载的配置文件

Ubuntu 中 Redis ,MySQL 基本使用-LMLPHP

客户端

连接redis:
redis-cli

运⾏测试命令:
ping

切换数据库
数据库没有名称,默认有16个,通过0-15来标识,连接redis默认选择第一个数据库
select 10

(2)基本操作

redis命令参考文档链接: link
http://doc.redisfans.com/

(3)与 python 交互

pip install redis

from redis import StrictRedis

if __name__=='__main__':
    try:
        sr = StrictRedis()
        # res = sr.set('name','itheima')
        # print(res)
        res=sr.set('name','itcast')
        print(res)
        res = sr.get('name')
        print(res)
    except Exception as e:
        print(e)
~                    

Ubuntu 中 Redis ,MySQL 基本使用-LMLPHP

2、MySQL

(1)打开数据库

mysql -uroot -p

(2)基本命令

show databases;
create database bj18 charset=utf8;
use bj18;
show tables;

(3)Django中MySQL数据库的配置

django-admin startproject name
python manage.py startapp appname
pip install pymysql

projectname/__ init __.py:

import pymysql
pymysql.install_as_MySQLdb()

settings.py:

DATABASES = {
    'default': {
        # 'ENGINE':'django.db.backends.sqlite3',
        "ENGINE":'django.db.backends.mysql',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        "NAME":'bj18',  # 使用的数据库的名字,必须手动创建
        "USER":'root',  # 链接mysql的用户名
        "PASSWORD":'mySQL',  # 用户对应的密码
        "HOST":'localhost',  # 指定mysql数据库所在电脑ip
        "PORT":3306,  # 端口号
    }
}

myapp/models.py

from django.db import models

# Create your models here.
class BookInfo(models.Model):
    btitle = models.CharField(max_length=20)
    bpub_date = models.DateField()
    bread = models.IntegerField(default=0)
    bcomment = models.IntegerField(default=0)
    isDelete = models.BooleanField(default=False)

class HeroInfo(models.Model):
    hname = models.CharField(max_length=20)
    hgender =models.BooleanField(default=False)
    hcomment =models.CharField(max_length=200)
    hbook = models.ForeignKey('BookInfo')
    isDelete = models.BooleanField(default=False)

python manage.py makemigrations
python manage.py migrate

与 MySQL 进行交互

show tables
desc booktest_bookinfo;
insert into booktest_bookinfo(btitle, bpub_date, bread, bcomment, isDelete) values
('射雕英雄传', '1980-5-1', 32, 34, 0),
('天龙八部', '1986-7-24', 36, 40, 0),
('笑傲江湖', '1995-12-24', 20, 80, 0),
('雪山飞狐', '1987-11-11', 58, 24, 0);

insert into booktest_heroinfo(hname, hgender, hbook_id, hcomment, isDelete) values
('郭靖', 1, 1, '降龙十八掌', 0),
('黄蓉', 0, 1, '打狗棍法', 0),
('黄药师', 1, 1, '弹指神通', 0),
('欧阳锋', 1, 1, '蛤蟆功', 0),
('梅超风', 0, 1, '九阴白骨爪', 0),
('乔峰', 1, 2, '降龙十八掌', 0),
('段誉', 1, 2, '六脉神剑', 0),
('虚竹', 1, 2, '天山六阳掌', 0),
('王语嫣', 0, 2, '神仙姐姐', 0),
('令狐冲', 1, 3, '独孤九剑', 0),
('任盈盈', 0, 3, '弹琴', 0),
('岳不群', 1, 3, '华山剑法', 0),
('东方不败', 0, 3, '葵花宝典', 0),
('胡斐', 1, 4, '胡家刀法', 0),
('苗若兰', 1, 4, '黄衣', 0),
('程灵素', 0, 4, '医术', 0),
('袁紫衣', 0, 4, '六合拳', 0);
select * from booktest_bookinfo;
select * from booktest_heroinfo;

案例——续上

创建模板文件夹

templates/appname/index.html
配settings.py templates部分

'DIRS': [os.path.join(BASE_DIR,'templates')],
创建视图函数,连接 models 和 templates (MVT)
from django.shortcuts import render
from booktest.models import BookInfo
# Create your views here.
def index(request):
    books = BookInfo.objects.all()
    return render(request,'booktest/index.html',{'books':books})
配置url

projectname / url.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r"^",include('booktest.urls')),
]

appname / urls.py

from django.conf.urls import url
from booktest import views

urlpatterns = [
    url(r'^index$',views.url),
]
完善模板文件
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>图书管理首页</title>
</head>
<body>
<a href="/create">新增</a>
<ul>
    {%  for book in books %}
        <li>{{ book.btitle }}</li>
    {%  endfor  %}
</ul>

</body>
</html>
完成首页超链接“新增”功能

appname / views.py

from django.http import HttpResponseRedirect
from datetime import date
def create(request):
    b = BookInfo()
    b.btitle = '流星蝴蝶剑'
    b.bpub_date = date(1990,1,1)
    b.save()
    # return HttpResponse('ok')
    return HttpResponseRedirect('index')

appname / urls.py

urlpatterns = [
    url(r'^index$',views.index),
    url(r'^create$',views.create),
]

ps :HttpResponseRedirect重定向体现在:
Ubuntu 中 Redis ,MySQL 基本使用-LMLPHP

完成首页超链接删除功能

完善 index.html

<li>{{ book.btitle }}--<a href="/delete{{ book.id }}">删除</a></li>

完成 views.delete 函数

def delete(request,bid):
    book = BookInfo.objects.get(id=bid)
    book.delete()
    return HttpResponseRedirect('/index')

完成 url 配置 appname / urls.py

urlpatterns = [
    url(r'^index$',views.index),
    url(r'^create$',views.create),
    url(r'^delete(\d+)',views.delete),
]

HttpResponseRedirect 相当于 redirect (from django import)

注意 url 配置时 / 的作用

在编辑 HTML 文件时

<a href='/create'>新增 </a>

/ 的作用是代替域名 如 127.0.0.1:8000/
正确的地址是 127.0.0.1:8000/create
如果没有 / 将是在原本的页面地址上拼接 create
如:当此时位于首页(127.0.0.1:8000/index)
点击新增,匹配的 url 将会是 127.0.0.1:8000/index/create

10-05 12:40