def file_create(request, help_id=0): help_id = int(help_id) if 0 == help_id: help_id = int(request.GET.get('id', 0)) if help_id > 0: res = Help.objects.filter(id=help_id) else: res = Help.objects.all() file_tpl = open(r'%s/help/content.html' % TEMPLATE_DIRS[0], 'r') tpl_content = file_tpl.read() file_tpl.close() t = Template(tpl_content) save_path = r'%s/help' % MEDIA_ROOT mkdir(save_path) for item in res: sign = '%s' % item.filename title = item.title content = filter_content(item.content) static_file_path = r'%s/%s.html' % (save_path, sign) delete_file(static_file_path) fileHandle = open(static_file_path, 'w') c = Context({"title": title, "content": content}) c = t.render(c) fileHandle.write(c.encode('utf-8')) fileHandle.close() cgname = HelpCategory.objects.all() if help_id == 0: file_tpl = open(r'%s/help/index.html' % TEMPLATE_DIRS[0], 'r') tpl_content = file_tpl.read() file_tpl.close() t = Template(tpl_content) data_list = [] cg = HelpCategory.objects.all() for item in cg: category = {} helps = Help.objects.filter(parent_id=item.id) category['name'] = item.name category['helps'] = helps data_list.append(category) index_html_path = r'%s/index.html' % (MEDIA_ROOT + '/help') delete_file(index_html_path) fileHandle = open(index_html_path, 'w') c = Context({"data_list": data_list}) c = t.render(c) fileHandle.write((c).encode('utf-8')) fileHandle.close() return render_to_response('help/list.html', {'res': res, 'cgname': cgname})
def create_notice_html(request, model_id, title, content): gl_path = GlobalPathCfg() file_name = '%s.html' % model_id file_url = gl_path.get_notice_html_url(request, file_name) save_path = gl_path.get_notice_html_save_path(file_name) file_tpl = open(gl_path.get_notice_html_template_path(), 'r') tpl_content = file_tpl.read() file_tpl.close() t = Template(tpl_content) html_file = open(save_path, 'w') c = Context({"title": title, "content": content}) c = t.render(c) html_file.write(c.encode('utf-8')) html_file.close() return file_url
def create_agreeinfo_html_file(content, use_template=True): '''创建公告模版 ''' # gl_path = _globalpathcfg file_name = 'argreeinfo.html' save_path = '%s/argreeinfo/argreeinfo.html' % STATIC_ROOT template_path = '%s/argreeinfo/argreeinfo_template.html' % STATIC_ROOT file_tpl = open(template_path, 'r') tpl_content = file_tpl.read() file_tpl.close() t = Template(tpl_content) html_file = open(save_path, 'w') if use_template: c = Context({"title": "用户协议", "content": content}) c = t.render(c) else: c = content # 只保存内容,不使用模版 html_file.write(c.encode('utf-8')) html_file.close() return file_name