Django 基础知识

发布时间: 更新时间: 总字数:803 阅读时间:2m 作者: 分享

Django 基础知识汇总

使用

安装

pip install django

创建新项目

django-admin.py startproject my_project

创建新的App

# 在Django项目(my_project)的根目录下执行
python manage.py startapp my_app

创建用户

python manage.py createsuperuser

http://127.0.0.1:8888/admin

进入 shell 调试

python manage.py shell
from moments.models import Status
statuses = Status.objects.all()
print statuses.query
print statuses
statuses[0].user

启动服务

python manage.py runserver 80

数据库操作

配置

测试环境,没有mysql时,可以将 mysql 切换为 sqlite3

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',  # 默认用mysql
        ...
    }
}

升级数据库

# make new migrations
python manage.py makemigrations
 
# apply all migrations
python manage.py migrate

显示 SQL

$ python manage.py sqlmigrate my_app 0001
BEGIN;
--
-- Create model Status
--
CREATE TABLE "moments_status" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "text" varchar(140) NOT NULL, "pics" varchar(400) NULL, "pub_time" datetime NOT NULL);
--
-- Create model WeChatUser
--
CREATE TABLE "moments_wechatuser" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "motto" varchar(500) NULL, "pic" varchar(200) NULL, "region" varchar(50) NULL, "user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id"));
--
-- Add field user to status
--
ALTER TABLE "moments_status" RENAME TO "moments_status__old";
CREATE TABLE "moments_status" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "text" varchar(140) NOT NULL, "pics" varchar(400) NULL, "pub_time" datetime NOT NULL, "user_id" integer NOT NULL REFERENCES "moments_wechatuser" ("id"));
INSERT INTO "moments_status" ("text", "pub_time", "user_id", "id", "pics") SELECT "text", "pub_time", NULL, "id", "pics" FROM "moments_status__old";
DROP TABLE "moments_status__old";
CREATE INDEX "moments_status_user_id_d38910b1" ON "moments_status" ("user_id");
COMMIT;

celery

启动

python  manage.py  celery  worker  --settings=settings -l info
python  manage.py  celerybeat --settings=settings

使用

from home_application import celery_tasks
celery_tasks.async_task(5, 8)        # 实时输出
celery_tasks.async_task.dely(5, 8)   # 延时执行
celery_tasks.async_task.apply_async(args=(5, 8))
import datatime
celery_tasks.async_task.apply_async(args=(5, 8), eta=datatime.datatime.now() + datatime.timedeta(seconds=60)) # 指定时间执行

celery 添加 task 后,需要在 conf/default.py 中,添加 CELERY_IMPORTS

Django Admin

ModelAdmin

作用:列表页优化和排序,admin.py如下:

from django.contrib import admin

class ArticleControl(admin.ModelAdmin):
    # 显示的字段
    list_display = ('title', 'body', 'auth', 'create_time', 'update_time')

    # 搜索条件
    search_fields = ('title',)

    # 按字段排序 -表示降序
    ordering = ('-create_time',)

    # 每页显示10条
    list_per_page = 10

    # 可编辑字段
    list_editable = ('auth',)

    # 设置哪些字段可以点击进入编辑界面
    list_display_links = ('title', 'body')

    # 点右侧作者名称,快速找到相关内容
    list_filter = ('auth', 'title')

    # 时间分层
    date_hierarchy = 'create_time'

    # 控制actions的下拉框出现在页面的位置
    actions_on_bottom = True
    actions_on_top = True
    # 是否在actions下拉框右侧显示选中的对象的数量
    actions_selection_counter = True


# 注册Article表
admin.site.register(models.Article, ArticleControl)

from django.contrib import admin


class BaseControl(admin.ModelAdmin):
    ordering = ('-id',)
    list_per_page = 20


class ArticleControl(BaseControl):
    # 显示的字段
    list_display = ('title', 'body', 'auth', 'create_time', 'update_time')


# 注册Article表
admin.site.register(models.Article, ArticleControl)

工具

根据模型字段,生成:"A", "B", "C",这种格式的字符串。

python manage.py shell

from home_application.models import *

def gen_doc(obj):
    l = obj.__dict__["__doc__"].replace(" ", "").split("(")[1].split(")")[0].split(",")
    print l
    r = ""
    for i in l:
        r += "\""
        r += i
        r += "\", "
    print r


gen_doc(Object)

错误

升级问题

django.db.utils.InternalError: (1050, u"Table 'django_content_type' already exists") 错误:

  File "/cache/.bk/env/lib/python2.7/site-packages/pymysql/connections.py", line 1138, in read
    first_packet = self.connection._read_packet()
  File "/cache/.bk/env/lib/python2.7/site-packages/pymysql/connections.py", line 906, in _read_packet
    packet.check_error()
  File "/cache/.bk/env/lib/python2.7/site-packages/pymysql/connections.py", line 367, in check_error
    err.raise_mysql_exception(self._data)
  File "/cache/.bk/env/lib/python2.7/site-packages/pymysql/err.py", line 120, in raise_mysql_exception
    _check_mysql_exception(errinfo)
  File "/cache/.bk/env/lib/python2.7/site-packages/pymysql/err.py", line 115, in _check_mysql_exception
    raise InternalError(errno, errorvalue)
django.db.utils.InternalError: (1050, u"Table 'django_content_type' already exists")
------FAILURE: Migrate Database------
[172.19.11.14]20190409-162750 105   [JOB FAILURE]

解决办法:


pip install  -r  requirements.txt -i http://<url>/simple --trusted-host <url>
# 1. 执行
python manage.py migrate --fake
# 2. 再次执行
python manage.py migrate

完毕。

Home Archives Categories Tags Docs