def release(): category_list = NewsCategory.query.all() if request.method == 'GET': return render_template('news/user_news_release.html', category_list=category_list) elif request.method == 'POST': dict1 = request.form title = dict1.get('title') category_id = int(dict1.get('category')) summary = dict1.get('summary') content = dict1.get('content') news_pic = request.files.get('news_pic') if not all([title, category_id, summary, content, news_pic]): return render_template('news/user_news_release.html', category_list=category_list, msg='数据不能为空') filename = upload_pic(news_pic) news = NewsInfo() news.title = title news.category_id = category_id news.summary = summary news.content = content news.pic = filename news.user_id = session['user_id'] db.session.add(news) db.session.commit() return redirect('/user/newsList')
def release(): # 查询新闻分类 # category = NewsCategory.query.all() if request.method == 'GET': return render_template('news/user_news_release.html', category=category) elif request.method == 'POST': dict1 = request.form title = dict1.get('title') category_id = dict1.get('category_id') summary = dict1.get('summary') content = dict1.get('content') pic = dict1.get('pic') print(dict1) if not all([title, category_id, summary, content, pic]): print(111) return jsonify(result=1, title=title, category_id=category_id, summary=summary, content=content, pic=pic) # p=pic # str1=p # us=re.split(r'\\',str1) # print(us) news = NewsInfo() print(news) news.category_id = category_id news.pic = pic str1 = news.pic use = re.split(r"\\", str1) print(use[2]) news.pic = use[2] news.title = title news.summary = summary news.content = content news.user_id = session['user_id'] db.session.add(news) db.session.commit() return jsonify(result=2)
def release(): # 查询所有的分类,供编辑人员选择 category_list = NewsCategory.query.all() # 接收新闻的编号, 为了确认是创建还是修改 news_id = request.args.get('news_id') if request.method == 'GET': if news_id is None: # 展示页面 return render_template('news/user_news_release.html', category_list=category_list, news=None) else: # 如果有新闻编号存在,则进行修改操作,所以需要查询到原新闻并展示 news = NewsInfo.query.get(int(news_id)) return render_template('news/user_news_release.html', category_list=category_list, news=news) elif request.method == 'POST': # 新闻的添加处理 # 1.接收请求 dict1 = request.form title = dict1.get('title') category_id = dict1.get('category') summary = dict1.get('summary') content = dict1.get('content') # 接收新闻图片 news_pic = request.files.get('news_pic') if news_id is None: # 2.验证 if not all([title, category_id, summary, content, news_pic]): return render_template('news/user_news_release.html', category_list=category_list, msg='请将数据填写完整') else: if not all([title, category_id, summary, content]): return render_template('news/user_news_release.html', category_list=category_list, msg='请将数据填写完整') # 上传图片到七牛云, 修改文章内容的时候,默认不改图片,检查一下到底要不要改 if news_pic: from utils.qiniu_xjzx import upload_pic filename = upload_pic(news_pic) # 3.添加 if news_id is None: news = NewsInfo() else: news = NewsInfo.query.get(news_id) news.category_id = int(category_id) if news_pic: news.pic = filename news.title = title news.summary = summary news.content = content news.status = 1 news.update_time = datetime.now() news.user_id = session['user_id'] # 4.提交 db.session.add(news) db.session.commit() # 5.响应 return redirect('/user/newslist')