Django入门项目

1. 框架基础学习

首先跟着这篇文章学习,懂得了如何打印 hello world!

接着学会了如何运用这个框架的MVC模式。 文章地址: python.jobbole.com/87444/

2. 项目实践

2.1 项目描述

然后动手做了个Todo list 的小项目,源码地址:github.com/TsingXu430/… 欢迎 star... 数据存储和部署都在 leancloud 上,很方便。

参考了leancloud上的开源项目:Flask Todo Demo github地址:github.com/leancloud/f…

2.2 实践步骤:

  1. 先通过Django搭建一个project,在setting.py中增加 leancloud 的app_id,app_key等参数。
  2. 在根目录下新建views文件夹,新增todo.py,此为主要代码文件
  3. 在templates文件夹下新建todo.html文件,编写前端代码
  4. 在static文件夹下引入页面需要的 js,css, font, 图片
  5. 一切准备好后,编写 urls.py,增加第一个路由: url('^$', todo.show) 这句的意思是将首页路由指向todo.py中show方法
  6. 编写show方法, 因为需要查询 leancloud 云存储,所以要引入相关库
import leancloud
from leancloud import Object, Query, LeancloudError
from settings import LC_APP_ID, LC_APP_KEY

#定义Todo实例对象,可以理解为数据表
class Todo(Object):
    pass

#leancloud 初始化,传入app_id,app_key
Leancloud.init(LC_APP_ID, LC_APP_KEY)  

#一个 * 号代表list或者tuple,** 代表map或者dic(字典)
def show(request,**status):
    status = int(status.get('status', 0))
    try:
        todos = Query(Todo).add_descending('createdAt').equal_to('status', status).find()
    except LeanCloudError as e:
        todos = []
        print(e.error)
    todos_list = []

# leancloud query的结果是一个list,所以需要转换成dict
    for todo in todos:
        todo_list = {}
        todo_list['id'] = todo.get('objectId')
        todo_list['content'] = todo.get('content')
        todo_list['createAt'] = todo.get('createdAt')
        todos_list.append(todo_list)
    context = {'status': status, 'todos': todos_list}

#最终使用Django框架的render来渲染模板,传入参数为context
    return render(request, 'todos.html', context)
复制代码

数据在 leancloud 存储不需要新建数据表,直接调用实例对象就可以,所以很方便。 其中 **status 是因为这个参数不是必须的,而且是字典dict类型。

  1. 编写 todo.html 模板
{% if status == 0 %}
    <ul class="list-group">
      {% for todo in todos %}
        <li class="list-group-item">
          <form class="pull-right" action="{% url 'delete' todo.id 0 %}" method="post" id=delete-{{todo.id}}>
              {% csrf_token %}
            <span class="glyphicon glyphicon-trash todo-delete"></span>
          </form>
          <form action="{% url 'done' todo.id 0 %}" method="post" id="done-{{todo.id}}">
              {% csrf_token %}
            <span class="glyphicon glyphicon-unchecked"></span> {{ todo.content }}
          </form>
        </li>
      {% endfor %}
    </ul>
    {% elif status == 1 %}
    <ul class="list-group">
      {% for todo in todos %}
        <li class="list-group-item">
          <form class="pull-right" action="{% url 'delete' todo.id 1 %}" method="post" id=delete-{{todo.id}}>
              {% csrf_token %}
            <span class="glyphicon glyphicon-trash todo-delete"></span>
          </form>
          <form action="{% url 'undone' todo.id 1 %}" method="post" id="undone-{{todo.id}}">
              {% csrf_token %}
            <span class="glyphicon glyphicon-check text-muted"></span> <del class="text-muted">{{ todo.content }}</del>
          </form>
        </li>
      {% endfor %}
    </ul>
    {% endif %}
复制代码

django会检测csrf攻击,所以在from表单中添加了 csrf_token,防止报错。

后面就是慢慢编写 delete, todo, untodo 这些方法。

  1. 提交到git后,发现github给这个项目标记的是html。原来需要添加配置 .gitattributes 文件:
*.js linguist-language=python
*.cdd linguist-language=python
*.html linguist-language=python
复制代码

到此,入门项目已经完成,接下去打算爬一些个人觉得有意思的数据做个web服务。

转载于:https://juejin.im/post/5bc1d09ef265da0a893056a5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值