Giter VIP home page Giter VIP logo

hellodjango-rest-framework-tutorial's Introduction


HelloDjango-REST-framework-tutorial
完全免费、开源的 HelloDjango 系列教程之 django REST framework 博客开发

WeiXin Sina Weibo

本项目延续自 HelloDjango-blog-tutorial,如果对 django 基础不是很熟悉,建议先学习 HelloDjango - Django博客教程(第二版),然后再进阶学习 django REST framework。

虽然项目延续自 HelloDjango-blog-tutorial,但只要你已有 django 基础(ORM、类视图、表单等),就可以直接开启本教程。两个教程在内容上并无联系,只是本教程借用了上一个教程的项目结构以及数据模型(Model)的定义。

分支说明

master 分支为项目的主分支,每一步关键功能的开发都对应一篇详细的教程,并和历史提交以及标签一一对应。例如第一篇教程对应第一个 commit,对应标签为 step1,依次类推。

资源列表

本地运行

可以使用 Virtualenv、Pipenv、Docker 等在本地运行项目,每种方式都只需运行简单的几条命令就可以了。

注意:

因为博客全文搜索功能依赖 Elasticsearch 服务,如果使用 Virtualenv 或者 Pipenv 启动项目而不想搭建 Elasticsearch 服务的话,请先设置环境变量 ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no 以关闭实时索引,否则无法创建博客文章。如果关闭实时索引,全文搜索功能将不可用。

Windows 设置环境变量的方式:set ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no

Linux 或者 macOS:export ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no

使用 Docker 启动则无需设置,因为会自动启动一个包含 Elasticsearch 服务的 Docker 容器。

无论采用何种方式,先克隆代码到本地:

$ git clone https://github.com/HelloGitHub-Team/HelloDjango-REST-framework-tutorial.git

Virtualenv

  1. 创建虚拟环境并激活虚拟环境,具体方法可参考基础教程中的:开始进入 django 开发之旅:使用虚拟环境

  2. 安装项目依赖

    $ cd HelloDjango-rest-framework-tutorial
    $ pip install -r requirements.txt
  3. 迁移数据库

    $ python manage.py migrate
  4. 创建后台管理员账户

    $ python manage.py createsuperuser

    具体请参阅基础教程中的 创作后台开启,请开始你的表演

  5. 运行开发服务器

    $ python manage.py runserver
  6. 浏览器访问 http://127.0.0.1:8000/admin,使用第 4 步创建的管理员账户登录后台发布文章,如何发布文章可参考基础教程中的:创作后台开启,请开始你的表演

    或者执行 fake 脚本批量生成测试数据:

    $ python -m scripts.fake

    批量脚本会清除全部已有数据,包括第 4 步创建的后台管理员账户。脚本会再默认生成一个管理员账户,用户名和密码都是 admin。

  7. 浏览器访问:http://127.0.0.1:8000,可进入到博客首页

Pipenv

  1. 安装 Pipenv(已安装可跳过)

    $ pip install pipenv
  2. 安装项目依赖

    $ cd HelloDjango-rest-framework-tutorial
    $ pipenv install --dev

    关于如何使用 Pipenv,参阅基础教程中:开始进入 django 开发之旅 的 Pipenv 创建和管理虚拟环境部分。

  3. 迁移数据库

    在项目根目录运行如下命令迁移数据库:

    $ pipenv run python manage.py migrate
  4. 创建后台管理员账户

    在项目根目录运行如下命令创建后台管理员账户

    $ pipenv run python manage.py createsuperuser

    具体请参阅基础教程中的 创作后台开启,请开始你的表演

  5. 运行开发服务器

    在项目根目录运行如下命令开启开发服务器:

    $ pipenv run python manage.py runserver
  6. 浏览器访问 http://127.0.0.1:8000/admin,使用第 4 步创建的管理员账户登录后台发布文章,如何发布文章可参考基础教程中的:创作后台开启,请开始你的表演

    或者执行 fake 脚本批量生成测试数据:

    $ pipenv run python -m scripts.fake

    批量脚本会清除全部已有数据,包括第 4 步创建的后台管理员账户。脚本会再默认生成一个管理员账户,用户名和密码都是 admin。

  7. 在浏览器访问:http://127.0.0.1:8000/,可进入到博客首页。

Docker

  1. 安装 Docker 和 Docker Compose

  2. 构建和启动容器

    $ docker-compose -f local.yml build
    $ docker-compose -f local.yml up
  3. 创建后台管理员账户

    $ docker exec -it hellodjango_rest_framework_tutorial_local python manage.py createsuperuser

    其中 hellodjango_rest_framework_tutorial_local 为项目预定义容器名。

  4. 浏览器访问 http://127.0.0.1:8000/admin,使用第 3 步创建的管理员账户登录后台发布文章,如何发布文章可参考基础教程中的:创作后台开启,请开始你的表演

    或者执行 fake 脚本批量生成测试数据:

    $ docker exec -it hellodjango_rest_framework_tutorial_local python -m scripts.fake

    批量脚本会清除全部已有数据,包括第 3 步创建的后台管理员账户。脚本会再默认生成一个管理员账户,用户名和密码都是 admin。

  5. 为 fake 脚本生成的博客文章创建索引,这样就可以使用 Elasticsearch 服务搜索文章

    $ docker exec -it hellodjango_rest_framework_tutorial_local python manage.py rebuild_index

    通过 admin 后台添加的文章会自动创建索引。

  6. 在浏览器访问:http://127.0.0.1:8000/,可进入到博客首页。

线上部署

拼命撰写中...

教程目录索引

  1. 开篇
  2. django-rest-framework 是什么鬼?
  3. 初始化 RESTful API 风格的博客系统
  4. 实现博客首页文章列表 API
  5. 用类视图实现首页 API
  6. 使用视图集简化代码
  7. 分页
  8. 文章详情 API
  9. 在接口返回Markdown解析后的内容
  10. 实现分类、标签、归档日期接口
  11. 评论接口
  12. 基于 drf-haystack 实现文章搜索接口
  13. 加缓存为接口提速
  14. API 版本管理
  15. 限制接口访问频率
  16. 单元测试
  17. 自动生成接口文档

公众号


欢迎关注 HelloGitHub 公众号,获取更多开源项目的资料和内容。

QQ 群

加入 QQ 群和更多的 django 开发者进行交流:

Django学习小组主群:696899473

版权声明

知识共享许可协议
本作品采用署名-非商业性使用-禁止演绎 4.0 国际 进行许可。

hellodjango-rest-framework-tutorial's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

hellodjango-rest-framework-tutorial's Issues

[BUG]alphine的dockerfile应该加上gcc

否则构建带drf-yacs的容器,会依赖的ruamel.yaml.clib-0.2.6会报无法编译:
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-91vjgeeu/ruamel.yaml.clib/setup.py'"'"'; file='"'"'/tmp/pipn'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-eg
cwd: /tmp/pip-install-91vjgeeu/ruamel.yaml.clib/
Complete output (3 lines):
sys.argv ['/tmp/pip-install-91vjgeeu/ruamel.yaml.clib/setup.py', 'egg_info', '--egg-base', '/tmp/pip-pip-egg-info-f1t97jmm']
test compiling /tmp/tmp_ruamel_f8rc27v5/test_ruamel_yaml.c -> test_ruamel_yaml compile error: /tmp/tmp_ruamel_f8rc27v5/test_ruamel_yaml.c
Exception: command 'gcc' failed with exit status 1

DockerFile加上gcc:
RUN apk update \

Pillow dependencies \

&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev gcc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.