学了两周的Django,跟着做了一个药品管理系统
系统项目结构如下:  bysms是项目根目录,common是关于数据的app,类似于类吧,mgr是管理员的包,sale是客户的包,z_list是前端的页面,之前做过javaweb以及windows应用的开发,发现计算机的天才太多了,他们总能发明出不同角度解决问题的办法,我也是入门,发现真的是mvc的结构分明,带给我很大启发
准备开始学是两周前吧,经历了很多坑就如学别的语言或框架一样,不会就百度,终于还是把manage.py,setting.py views.py ,urls.py搞清楚了,之前光知道计算机网络里有路由,才发现Django还有这玩意,总路由,子路由的搭配
学了这个我对http https以及web中的请求行,响应行,状态码,消息体,以及Html,css等都有回顾,我知道了ssl协议,知道了统一资源定位符等结构的内容
就放出一个管理员的对客户管理的代码:
from django.http import JsonResponse
import json
from common.models import Customer
def dispatcher(request):
if 'usertype' not in request.session:
return JsonResponse({
'ret': 302,
'msg': '未登录',
'redirect': '/mgr/sign.html'},
status=302)
if request.session['usertype'] != 'mgr':
return JsonResponse({
'ret': 302,
'msg': '用户非mgr类型',
'redirect': '/mgr/sign.html'},
status=302)
if request.method == 'GET':
request.params = request.GET
elif request.method in ['POST','PUT','DELETE']:
request.params = json.loads(request.body)
action = request.params['action']
if action == 'list_customer':
return listcustomers(request)
elif action == 'add_customer':
return addcustomer(request)
elif action == 'modify_customer':
return modifycustomer(request)
elif action == 'del_customer':
return deletecustomer(request)
else:
return JsonResponse({'ret': 1, 'msg': '不支持该类型http请求'})
pass
def listcustomers(request):
qs = Customer.objects.values()
retlist = list(qs)
return JsonResponse({'ret': 0, 'retlist': retlist})
def addcustomer(request):
info = request.params['data']
record = Customer.objects.create(name=info['name'] ,
phonenumber=info['phonenumber'] ,
address=info['address'])
return JsonResponse({'ret': 0, 'id':record.id})
def modifycustomer(request):
customerid = request.params['id']
newdata = request.params['newdata']
try:
customer = Customer.objects.get(id=customerid)
except Customer.DoesNotExist:
return {
'ret': 1,
'msg': f'id 为`{customerid}`的客户不存在'
}
if 'name' in newdata:
customer.name = newdata['name']
if 'phonenumber' in newdata:
customer.phonenumber = newdata['phonenumber']
if 'address' in newdata:
customer.address = newdata['address']
customer.save()
return JsonResponse({'ret': 0})
def deletecustomer(request):
customerid = request.params['id']
try:
customer = Customer.objects.get(id=customerid)
except Customer.DoesNotExist:
return {
'ret': 1,
'msg': f'id 为`{customerid}`的客户不存在'
}
customer.delete()
return JsonResponse({'ret': 0})

这个项目有seesion的机制,所以他每次会判断你是否已经登录了,如果你没有登录是不能操作:
if 'usertype' not in request.session:
return JsonResponse({
'ret': 302,
'msg': '未登录',
'redirect': '/mgr/sign.html'},
status=302)
if request.session['usertype'] != 'mgr':
return JsonResponse({
'ret': 302,
'msg': '用户非mgr类型',
'redirect': '/mgr/sign.html'},
status=302)
数据库用的是sqlite: 
项目运行: 首页: 
登录成功:
 登录失败: 
 接下来计划写一个win32的聊天室,已经开始写好了外层界面的第一步,不过win32资料文档好少,寒假看能写完不,下周还考无线传感器,太烦了,这课实验还挺有用,剩下就是概念还有背,老师一学期基本都在吹他的曾经的学生以及培训班有多好,害
|