第十七篇:django基础(二)
本篇内容
简单图书crm系统
编写views
views:作为MVC中的C,接收用户的输入,调用数据库Model层和业务逻辑Model层,处理后将处理结果渲染到V层中去。
app01/views.py:
from django.shortcuts import render, HttpResponse, redirect # Create your views here. from app01 import models def index(request):
bookList = models.Book.objects.all()
return render(request, "app01/index.html", {"bookList": bookList}) def add(request):
if request.method == "POST":
title = request.POST.get("title")
pubdate = request.POST.get("pubdate")
price = request.POST.get("price")
publish = request.POST.get("publish")
models.Book.objects.create(title=title, pubDate=pubdate, price=price, publish=publish)
return redirect("/index")
return render(request, "app01/add.html") def delbook(request, id):
models.Book.objects.filter(id=id).delete()
return redirect("/index") def editbook(request, id):
if request.method == "POST":
title = request.POST.get("title")
pubdate = request.POST.get("pubdate")
price = request.POST.get("price")
publish = request.POST.get("publish")
models.Book.objects.filter(id=id).update(title=title, pubDate=pubdate, price=price, publish=publish)
return redirect("/index/")
edit_book = models.Book.objects.filter(id=id)[0]
return render(request, "app01/edit.html", {"edit_book": edit_book})
编写urls
urls,程序的入口,支持正则匹配访问url,将访问url映射到views中的具体某个函数中。
为了能调用到上面这个views,我们需要将views.index函数映射到URL中。
我们可以创建一个urls.py 在App目录下。
app01/urls.py:
"""crm URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^add/', views.add),
url(r'^del/(\d+)', views.delbook),
url(r'^edit/(\d+)', views.editbook),
]
编写models
models与数据库操作相关,是django处理数据库的一个特色之处,它包含你的数据库基本字段与数据。通过一系列封装的api可以直接操作数据库。当然,也支持原生sql。
既然models与数据库相关,那么首先需要配置数据库
1、数据库设置,mysite/settings.py:
这里默认使用内置的sqlite3,配置如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
app01/models.py:
from django.db import models # Create your models here. class Book(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=32)
pubDate = models.DateField()
price = models.DecimalField(max_digits=6, decimal_places=2)
publish = models.CharField(max_length=32)
控制台分别运行:
$ python manage.py makemigrations
$ python manage.py migrate
编写html
app01/templates/index:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主页</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7/dist/css/bootstrap.css">
<style>
.container{
margin-top: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<a href="/add/"><button class="btn btn-primary">添加</button></a>
<table class="table table-hover">
<tr>
<th>编号</th>
<th>书名</th>
<th>出版日期</th>
<th>价格</th>
<th>出版社</th>
<th>操作</th>
</tr>
{% for book_obj in bookList %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.pubDate | date:"Y-m-d"}}</td>
<td>{{ book_obj.price }}</td>
<td>{{ book_obj.publish }}</td>
<td>
<a href="/edit/{{ book_obj.id }}"><button class="btn btn-info">编辑</button></a>
<a href="/del/{{ book_obj.id }}"><button class="btn btn-danger">删除</button></a>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</body>
</html>
app01/templates/add:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加页面</title>
</head>
<body>
<form action="/add/" method="post">
<p>书名:<input type="text" name="title"></p>
<p>出版日期:<input type="date" name="pubdate"></p>
<p>价格:<input type="text" name="price"></p>
<p>出版社:<input type="text" name="publish"></p>
<input type="submit">
</form>
</body>
</html>
app01/templates/edit:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编辑页面</title>
</head>
<body>
<form action="/edit/{{ edit_book.id }}" method="post">
<p>书名:<input type="text" name="title" value="{{ edit_book.title }}"></p>
<p>出版日期:<input type="date" name="pubdate" value="{{ edit_book.pubDate|date:'Y-m-d' }}"></p>
<p>价格:<input type="text" name="price" value="{{ edit_book.price }}"></p>
<p>出版社:<input type="text" name="publish" value="{{ edit_book.publish }}"></p>
<input type="submit">
</form>
</body>
</html>
第十七篇:django基础(二)的更多相关文章
- day 66 Django基础二之URL路由系统
Django基础二之URL路由系统 本节目录 一 URL配置 二 正则表达式详解 三 分组命名匹配 四 命名URL(别名)和URL反向解析 五 命名空间模式 一 URL配置 Django 1.11 ...
- day 53 Django基础二之URL路由系统
Django基础二之URL路由系统 本节目录 一 URL配置 二 正则表达式详解 三 分组命名匹配 四 命名URL(别名)和URL反向解析 五 命名空间模式 一 URL配置 Django 1.11 ...
- Django基础二静态文件和ORM
Django基础二静态文件和ORM 目录 Django基础二静态文件和ORM 1. 静态文件 1.1 静态文件基本配置: 1.2 静态文件进阶配置 2. request参数 3. Django配置数据 ...
- 第一篇:Django基础
Django框架第一篇基础 一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model ...
- Django基础(二):环境配置
前戏 WEB框架简介 具体介绍Django之前,必须先介绍WEB框架等概念. web框架: 别人已经设定好的一个web网站模板,你学习它的规则,然后“填空”或“修改”成你自己需要的样子. 一般web框 ...
- Django 基础二(View和urls)
上一篇博文已经成功安装了python环境和Django,并且新建了一个空的项目.接下来就可以正式开始进行Django下 的Web开发了.首先进入项目的主目录: cd ./DjangoLearn/hol ...
- Django基础二之URL路由系统
一 URL配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.你就是以这 ...
- python 自动化之路 day 19 Django基础[二]
Django - 路由系统 url.py - 视图函数 views.py - 数据库操作 models.py - 模板引擎渲染 - HttpReponse(字符串) - render(request, ...
- 02.Django基础二之URL路由系统
一 URL配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.你就是以这 ...
- python django基础二URL路由系统
URL配置 基本格式 from django.conf.urls import url #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数执行,就不再往下循环了,并给函数 ...
随机推荐
- image retrieval数据集
1. Oxford,vgg组,主要是building方面的数据.http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/index.html 2. Calte ...
- centos7 多网卡修改默认路由
最近在virtualbox里搭了一个centos7的虚拟机,但是网络这一块总是有问题. 单网卡下的问题: 1.当我配置连接方式为NAT网络地址转换的时候,虚拟机可以访问外网.但是在网络地址转换的情况下 ...
- Linux CentOS 7的图形界面安装
1.首先安装X(X Window System) yum groupinstall "X Window System" 2.由于这个软件组比较大,安装过程会比较慢,安装完成后会出现 ...
- php获取设备的宽度和高度
php获取设备的宽度和高度 如果前台没有传输当前请求的宽度和高度,我们有时候需要用php借助javascript获取屏幕的宽和高,以控制现实的样式. 代码如下: <?php /* * 获取设备宽 ...
- linux系统集群之高可用(一)HA
HA(High aviliable)高可用 高可用的需求 在很多公司里面,都会存在着一些不愿被中断的业务,但是由于硬件故障,软件故障,人为因素等各种因素,往往会不经意的造成我们重要的业务中断,因此高可 ...
- 边缘检测 opencv
本次实验使用了两种方法进行了边缘检测,分别使用到了opencv中的两个API函数为Canny()和Sobel()函数.实验后加了Scharr滤波器,它其实是基于Sobel()函数的. 这三个API中的 ...
- 在intellij idea 里来回跳转查询方法
在intellij idea 里来回跳转查询方法,在不知道快捷键的时候真是抓狂. 看到key map中的快捷键 后退back ctrl +alt + ← 或者button4 click 前进forw ...
- Color Length UVA - 1625 DP
题目:题目链接 题意:输入两个长度分别为n和m的颜色序列,要求按顺序合并成同一个序列,即每次可以把一个序列开头的颜色放到新序列的尾部.对于每个颜色c来说,其跨度L(c)等于最大位置和最小位置之差,输出 ...
- 【数据库】MySQL 从安装到命令
一, MySQL 的安装于配置 我是通过百度云盘的方式下载的.建议登录百度云终端,然后点击下面的链接,选择要安装的版本,解压安装. http://www.h2ero.cn/pan/share/17cd ...
- saltstack执行远程命令
目录 Remote Execution salt state salt state 系统 salt state 系统流程 Runner salt runner Orchestrate Runner S ...