Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django学习记录(八):Django by example -- Blog(四) #44

Open
PyxYuYu opened this issue Sep 28, 2016 · 0 comments
Open

Django学习记录(八):Django by example -- Blog(四) #44

PyxYuYu opened this issue Sep 28, 2016 · 0 comments
Labels

Comments

@PyxYuYu
Copy link
Owner

PyxYuYu commented Sep 28, 2016

Love all, trust a few, do wrong to none.

0x01 Django

  • Build a Blog Application

    • Post模块解析
      • titlepost title 字段,字符型字段,数据库中用于转换
      • slug : 在 URLs 中使用
      • author
    • 安装pytz模块
      • 模型提供了 timezoneSQLite 处理日期时间时需要用到
      • settings.py 中可以利用 USE_TZ 命令设置激活或者不激活时区功能
    • Activating your application
      • 激活应用,在 settins.py 中的 INSTALLED_APPS 内添加 blog 应用
    INSTALLED_APPS = [
    ...
    ...
    ...
    'blog', 
    ]
    • Creating and applying migrations

    • migrations 数据迁移工具

      • migrate 用于执行迁移动作
      • makemigrations 基于当前的 model 创建新的迁移策略文件
      • sqlmigrate 显示迁移的 SQL 语句
      • migration 是基于 APP 的, 所以可以针对某些 APP 不启用 migration 功能
    • 返回到项目根目录,输入以下命令

      • python manage.py makemigrations blog
      • 显示如下:
      Migrations for 'blog':
      0001_initial.py:
      - Create model Post
      
      • blog 这个 app 下建立了一个 migrations 文件夹,创建了 0001_initial.py 文件,其中记录了所有 models.py 的改动(创建了 Post 模块)

      • python manage.py sqlmigrate blog 0001 查看该 migrations 对于于什么样的 sql 语句

      • 显示如下:

        
        BEGIN;
        --
        -- Create model Post
        --
        CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(250) NOT
        NULL, "slug" varchar(250) NOT NULL, "body" text NOT NULL, "publish" datetime NOT NULL, "created" dat
        etime NOT NULL, "updated" datetime NOT NULL, "status" varchar(10) NOT NULL, "author_id" integer NOT
        NULL REFERENCES "auth_user" ("id"));
        CREATE INDEX "blog_post_2dbcba41" ON "blog_post" ("slug");
        CREATE INDEX "blog_post_4f331e2f" ON "blog_post" ("author_id");
        COMMIT;
        
        
      • python manage.py migrate 将之前的改动同步到数据库文件

      • 显示如下:

      Operations to perform:
      Apply all migrations: admin, blog, contenttypes, auth, sessions
      Running migrations:
      Rendering model states... DONE
      Applying blog.0001_initial... OK
      
      • 如果之后 models 有新的改动,需要再次调用 makemigrations 命令

      • Creating an administration site for your models

      • 利用 django.contrib.admin, 已经包括在 INSTALLED_APPS

      • Creating a superuser

      • 创建管理员,python manage.py createsuperuser

      • 显示如下:

        Username (leave blank to use 'administrator'): admin
        Email address: [email protected]
        Password:
        Password (again):
        This password is too short. It must contain at least 8 characters.
        Password:
        Password (again):
        This password is too common.
        Password:
        Password (again):
        Superuser created successfully.
        
        
      • python manage.py runserver 就可以打开 http://localhost:8000/admin 查看,登录

      • Adding your models to the administration site

      • blog 中的模型添加到管理员站点,编辑 admin.py 文件

      from django.contrib import admin
      from .models import Post
      #
      admin.site.register(Post)
      • 无需重启服务器,只需要刷新站点就可以看到模型已经更新到站点
      • Customizing the way models are displayed
      • 定制管理站点,编辑 admin.py
from django.contrib import admin
from .models import Post

class PostAdmin(admin.ModelAdmin):
    # 列表属性
    list_display = ('title', 'slug', 'author',
                    'publish', 'status')
    # 最右边一列属性(过滤器)
    list_filter = ('status', 'created', 'publish', 'author')
    # 上边的搜索属性
    search_fields = ('title', 'body')
    # 预填充属性
    prepopulated_fields = {'slug': ('title', )}
    raw_id_fields = ('author', )
    # 日期导航属性
    date_hierarchy = 'publish'
    # 排列顺序属性
    ordering = ['status', 'publish']

admin.site.register(Post, PostAdmin)
@PyxYuYu PyxYuYu added the Django label Sep 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant