美文网首页
django学习笔记

django学习笔记

作者: ironman_ | 来源:发表于2018-04-29 00:29 被阅读0次

    virtualenv

    安装virtualenv,一个依赖管理工具

    pip3 install virtualenv
    

    创建名称为env的工作空间(文件夹)

    virtualenv -p /usr/local/bin/python3 env
    # 或者
    virtualenv venv
    

    进入虚拟环境

    source env/bin/activate
    

    退出虚拟环境

    deactivate
    

    在对应virtualenv环境下安装依赖包

    pip install Django
    pip install djangorestframework
    //安装4.0.0b11版本的web3
    pip install web3==4.0.0b11
    //安装bs4,用来解析网页数据,根据具体业务来,可以不需要安装
    pip install beautifulsoup4
    //进行网络请求的库
    pip install urllib3
    //支持https的库
    pip install certifi
    

    django相关

    查看安装的django版本号

    python -m django --version
    

    创建工程

    django-admin startproject mysite
    

    创建app

    python manage.py startapp polls
    

    算了自己看吧
    https://docs.djangoproject.com/en/2.0/intro/tutorial01/

    改动了model之后需要运行的

    //根据model在migrations文件夹中生成对应的代码
    python manage.py makemigrations
    //对数据库执行对应表的修改,比如增加字段,增加默认值等等
    python manage.py migrate
    

    在特定ip和端口上运行

    python manage.py runserver
    python manage.py runserver 0.0.0.0:8000
    

    默认是127.0.0.1,这是localhost的访问
    0.0.0.0是指开放都任意端口

    0.0.0.0:80 is a shortcut meaning "bind to all IP addresses this computer supports". 127.0.0.1:80 makes it bind only to the "lo" or "loopback" interface. If you have just one NIC with just one IP address, you could bind to it explicitly with, say, 192.168.1.1:80 (if 192.168.1.1 was your IP address), or you could list all the IPs your computer responds to, but 0.0.0.0:80 is a shortcut for that.

    https://stackoverflow.com/questions/1621457/about-ip-0-0-0-0-in-django

    运行可能出现的问题:

    如果出现Invalid HTTP_HOST header

    在settings.py 中添加对应的host,比如ALLOWED_HOSTS = ['*’],或者填写上具体的ip


    mysql数据库设置

    设置数据库为mysql

    //安装mysqlclient
    sudo apt-get install libmysqlclient-dev
    pip install mysqlclient
    

    添加配置文件mysql.cnf

    [client]
    database = database_name
    user = yourusername
    password = your_password
    host = localhost
    default-character-set = utf8
    character_set_connection=utf8
    collation_connection=utf8_unicode_ci
    storage_engine=INNODB
    

    将配置文件添加到settings.py

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'OPTIONS': {
                'read_default_file': os.path.join(BASE_DIR, 'mobi_wallet/mysql.cnf'),
            },
        }
    }
    

    django的数据库操作

    如果想看数据一般使用插件直接在pycharm上查看修改数据库

    filter和get的区别:

    get是获取到一个值,没有值会抛出`DoesNotExist`移除,有多个值会抛出`MultipleObjectsReturned`异常
    filter会返回一个QuerySet,可能为空可能有多个值
    

    如何捕捉get抛出的这两个异常:

    from django.core.exceptions import MultipleObjectsReturned
    # DoesNotExist是Model的一个属性,所以是可以从自己的model类中访问的,比如
     try:
        data = TransactionList.objects.get(txhash=txHash)
    except(TransactionList.DoesNotExist, MultipleObjectsReturned) as e:
        print(e) 
    

    创建数据:如果对应主键已经存在则是更新数据,所以create确切的说是insert_or_update

    TransactionList.objects.create(
        txhash=txhash
    )
    

    create操作等同于q =TransactionList(txhash=“xxx") , q.save() ,不用再额外的调用save操作了。

    筛选

    复杂筛选,比如or筛选需要用到Q类

    Poll.objects.get(
        Q(question__startswith='Who'),
        Q(pub_date=date(2005, 5, 2)) |Q(pub_date=date(2005, 5, 6))
    )
    

    //跟下面的sql类似

    SELECT *frompollsWHERE question LIKE 'Who%'
        AND (pub_date ='2005-05-02'OR pub_date ='2005-05-06')
    

    原文链接:https://docs.djangoproject.com/en/2.0/topics/db/queries/#complex-lookups-with-q


    部署django程序

    虽然django的开发程序自带容器,但是不够稳定,通常使用uwsgi来部署django程序

    安装wsgi

    pip install uwsgi
    

    添加启动配置文件:

    uwsginc_conf.ini

    [uwsgi]
    http = :9090
    chdir = /home/ubuntu/workspace/projects/myproject
    wsgi-file = myproject/wsgi.py
    processes = 4
    threads = 2
    stats = 127.0.0.1:9191
    

    注意将chdir的目录改成存放代码的目录

    使用上面的配置文件运行uwsgi

    uwsgi uwsginc_conf.ini
    //在后台进程中运行uwsgi
    nohup uwsgi uwsgi.ini &
    

    //查看运行的进程

    ps -aux | grep uwsgi
    # 可能会显示好几个进程
    

    停止(杀掉主进程就好了):

    kill -9 8888
    

    相关文章

      网友评论

          本文标题:django学习笔记

          本文链接:https://www.haomeiwen.com/subject/ksznlftx.html