动态 版块 发帖 消息 我的
xwylt
书山有路勤为径,学海无涯苦作舟
子版块
lytcreate
4
settings.py修改下列配置:LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai'
 0   0  5小时前
lytcreate
4
1、创建django项目,修改settings.py 为所有人允许访问:ALLOWED_HOSTS = ['*',]2、更改数据库为mysql(注释掉原来的数据库连接部分):DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST':'127.0.0.1', 'PORT':3306, 'NAME': 'aqk', //使用的数据库名字 'USER':'root', 'PASSWORD':'eeeee' } }修改__init__.pyimport pymysql pymysql.version_info = (1,4,13,'final',0) pymysql.install_as_MySQLdb()3、设置静态文件文件夹(css,js等),在项目文件夹下手动创建,settings.py里面补上:STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )4、如果有图片,视频,音乐等设置media文件夹,手动创建文件夹,settings.py补上:MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')5、在settings.py同级目录下创建 models.py,写入创建数据库相关语句from django.db import models class Person(models.Model): id = models.AutoField(primary_key=True) card = models.CharField(max_length=20) number = models.CharField(max_length=20) name = models.CharField(max_length=20) department = models.CharField(max_length=30)如需上传图片和文件,如下格式(photos为存放图片的文件夹,files为存放文件的文件夹,需手动创建):photoafter = models.ImageField(upload_to='photos',default='photos/moren.png') jfw = models.FileField(upload_to='files',default=None)6、在settings.py同级目录下,新建views.py用于后台函数编写,并在urls.py引用
 0   0  7小时前
lytcreate
78
def ceshi(request): if request.method == 'GET': conn = pymysql.connect(host=host, port=port, user=user, password=password, database=database, charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select * from auth_user") rows = cursor.fetchall() rowarray_list = [] for row in rows: t = (row['id'], row['password'], row['is_superuser'], row['username']) rowarray_list.append(t) g = json.dumps(rowarray_list) with open("F:\student_rowarrays.json", "w") as h: h.write(g) objects_list = [] for row in rows: d = collections.OrderedDict() d["id"] = row['id'] d["password"] = row['password'] d["is_superuser"] = row['is_superuser'] d["username"] = row['username'] objects_list.append(d) print(objects_list) for i in range(0,len(objects_list)): j = json.dumps(objects_list[i]) with open("F:\student_objects.json", "a") as f: f.write(j) conn.close() return render(request,'ceshi.html') else: return render(request,'ceshi.html')
 0   0  10天前
lytcreate
61
contents = models.CharField(max_length=16000) #最大值为16383
 1   0  10天前
lytcreate
56
pycharm打开命令行,进入项目所在的目录,输入python manage.py startapp article指令,创建名为article的app接着我们需要修改项目配置文件,“告诉”Django现在有article这么一个app了。打开my_blog目录的settings.py,找到INSTALLED_APPS写入如下代码:配置访问路径(urls)然后再给app配置访问路径urlurl可以理解为访问网站时输入的网址链接,配置好url后Django才知道怎样定位app打开my_blog目录下的urls.py,增加以下代码:path为Django的路由语法:参数article/分配了app的访问路径;include将路径分发给下一步处理;namespace可以保证反查到唯一的url,即使不同的app使用了相同的url(后面会用到)。现在我们已经通过path将根路径为article的访问都分发给article这个app去处理。但是app通常有多个页面地址,因此还需要app自己也有一个路由分发,也就是article.urls了。article可以有多个页面,如列表页面、详情页面等,那么就需要如下两个url: http://127.0.0.1:8000/article/list/ http://127.0.0.1:8000/article/detail/ app中的url.py就是用来区分它们的。在app生成时并没有这个文件,因此需要自己在article文件夹中创建urls.py,在里面输入:注意此时app还没有写好,因此启动服务器可能会报错,是正常的。 Django2.0之后,app的urls.py必须配置app_name,否则会报错。
 0   0  13天前
lytcreate
53
now = datetime.date.today() month = (now.month - 1) - (now.month - 1) % 3 + 1 thisstart = datetime.date(now.year, month, 1)#这里必须分情况,否则10月份时会时间溢出 if month <10: thisend = datetime.date(now.year, month + 3, 1) - datetime.timedelta(days=1) else: thisend = datetime.date(now.year+1, 1, 1) - datetime.timedelta(days=1) # 上个季度 lastend = thisstart - datetime.timedelta(days=1) laststart = datetime.date(lastend.year, lastend.month - 2, 1)
 0   0  16天前
lytcreate
139
报错:Exception at /sotdc/Attempt to overwrite cell: sheetname='SOT问题统计表' rowx=1 colx=9解决:检查表格数字,有重复单元格
 0   0  28天前
lytcreate
106
ws = Workbook(encoding='utf-8') # 设置Excel为UTF-8的编码格式w = ws.add_sheet("危险源辨识、风险评价与控制措施表") # 设置sheet名称style = xlwt.XFStyle()al = xlwt.Alignment()al.horz = 0x02 # 设置水平居中al.vert = 0x01 # 设置垂直居中style.alignment = al# 合并第1行的第0列到第17列,然后居中w.write_merge(0, 0, 0, 16, 'SGMW重庆分公司('+department+')危险源辨识、风险评价与控制措施表', style)下面是一个导出的实例:@login_required()def wxydc(request): if request.method == 'POST': department = request.POST.get('department') timestart = request.POST.get('timestart') timeend = request.POST.get('timeend') conn = pymysql.connect(host=host, port=port, user=user, password=password, database=database, charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select workplace,workcontent,dsdescription,dstype,maybetrouble,probability,exposuretime,resoluts,dzhi,risklevel,unaccept,adopted,controldocumet,ifimportantds,ifaccept,changes,dsdepartment from aqk_dangersource where intime between %s and %s and dsdepartment=%s ",[timestart,timeend,department,]) wxydc_list = cursor.fetchall() cursor.close() conn.close() ws = Workbook(encoding='utf-8') # 设置Excel为UTF-8的编码格式 w = ws.add_sheet("危险源辨识、风险评价与控制措施表") # 设置sheet名称 style = xlwt.XFStyle() al = xlwt.Alignment() al.horz = 0x02 # 设置水平居中 al.vert = 0x01 # 设置垂直居中 style.alignment = al # 合并第1行到第2行的第0列到第3列 w.write_merge(0, 0, 0, 16, 'SGMW重庆分公司('+department+')危险源辨识、风险评价与控制措施表', style) w.write_merge(1,1,0,4,'区域:'+department,style) w.write_merge(1, 1, 5, 12, '评估人员:各区域工段长、班组长、员工、安全工程师', style) w.write_merge(1, 1, 13, 16, '日期:', style) w.write_merge(2, 3, 0, 0, '序号', style) w.write_merge(2, 3, 1, 1, '作业活动场所', style) w.write_merge(2, 3, 2, 2, '作业活动内容', style) w.write_merge(2, 3, 3, 3, '危险源描述', style) w.write_merge(2, 3, 4, 4, '危险源类型', style) w.write_merge(2, 3, 5, 5, '可能导致的事故', style) w.write_merge(2, 2, 6, 9, '风险评价(LEC法)', style) w.write(3, 6, '可能性L', style) w.write(3, 7, '暴露时间E', style) w.write(3, 8, '事故后果C', style) w.write(3, 9, 'D值', style) w.write_merge(2, 3, 10, 10, '风险等级', style) w.write_merge(2, 3, 11,11, '不可接受风险直接判定', style) w.write_merge(2, 3, 12, 12, '已采取的控制措施', style) w.write_merge(2, 3, 13, 13, '相应的控制文件', style) w.write_merge(2, 3, 14, 14, '是否重要危险源', style) w.write_merge(2, 3, 15, 15, '是否可接受风险', style) w.write_merge(2, 3, 16, 16, '变更情况', style) # 把需要导出的数据写到文件中 excel_row = 4 dcb_row = 1 for dcb in wxydc_list: w.write(excel_row, 0, dcb_row) w.write(excel_row, 1, dcb['workplace']) w.write(excel_row, 2, dcb['workcontent']) w.write(excel_row, 3, dcb['dsdescription']) w.write(excel_row, 4, dcb['dstype']) w.write(excel_row, 5, dcb['maybetrouble']) w.write(excel_row, 6, dcb['probability']) w.write(excel_row, 7, dcb['exposuretime']) w.write(excel_row, 8, dcb['resoluts']) w.write(excel_row, 9, dcb['dzhi']) w.write(excel_row, 10, dcb['risklevel']) w.write(excel_row, 11, dcb['unaccept']) w.write(excel_row, 12, dcb['adopted']) w.write(excel_row, 13, dcb['controldocumet']) w.write(excel_row, 14, dcb['ifimportantds']) w.write(excel_row, 15, dcb['ifaccept']) w.write(excel_row, 16, dcb['changes']) excel_row += 1 dcb_row += 1 sio = BytesIO() # 写出到IO ws.save(sio) sio.seek(0) # 重新定位到开始 response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel') # 设置文件格式为Excel response['Content-Disposition'] = 'attachment; filename="危险源导出表.xls"' response.write(sio.getvalue()) return response else: return render(request,'wxydc.html')
 0   0  29天前
lytcreate
88
问题原因是:<form> 里面忘记写method方法 method=“post”</form>解决方案:添加即可!
 0   0  30天前
lytcreate
107
计算mysql很多行的和,用 sum(需要计算的变量)计算mysql某个条件有多少行,用 count(*)
 0   0  31天前
快速发帖 高级模式
我的小伙伴
Powered by Xwylt.com
© 2020-2020 lytcreate
您的IP:3.237.186.116,2020-10-21 20:57:06,Processed in 0.02184 second(s).
Powered by HadSky
书山有路勤为径,学海无涯苦作舟