def modal_add_tearch(request): context = {} if request.method == 'GET': import time time.sleep(1) sqlheper = SqlHeper() class_list = sqlheper.select_all_data("select id,title from classes", []) sqlheper.close_data() context['class_list'] = class_list else: try: t_name = request.POST.get('t_name') class_id_list = request.POST.getlist('class_id_list') sqlheper = SqlHeper() t_id = sqlheper.lastrowid("insert into tearchs (tname)value(%s)", [ t_name, ]) t_c_id = list(map(lambda x: (t_id, x), class_id_list)) sqlheper.insert_many_data( "insert into tearch2class (tearch_id,class_id)value(%s,%s)", t_c_id) sqlheper.commit_data() sqlheper.close_data() context = {} context['status'] = 'success' except Exception as e: context['status'] = 'error' context['message'] = str(e) return JsonResponse(context)
def add_tearch(request): sqlhepr = SqlHeper() if request.method == "POST": tearch_name = request.POST.get('add_tearchname') class_ids = request.POST.getlist('class_id') tearch_id = sqlhepr.lastrowid("insert into tearchs (tname) value(%s)", [ tearch_name, ]) class_many_id = map(lambda x: (tearch_id, x), class_ids) sqlhepr.insert_many_data( "insert into tearch2class (tearch_id,class_id) value(%s,%s)", class_many_id) sqlhepr.commit_data() sqlhepr.close_data() return redirect('/tearchs/') else: import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='******', passwd='root', db='test', charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("select * from classes") class_list = cursor.fetchall() context = {} context['class_list'] = class_list return render(request, 'add_tearch.html', context)