Django前端的文本编辑器,滑动登陆
文本编译器
# 添加文章 url(r'^addarticle/$', views.addarticle),
# 利用文本编辑器添加文章 def addarticle(request): ''' 添加文章 :param request: :return: ''' if request.method == 'POST': article_form=ArticleForm(request.POST) if article_form.is_valid(): title = article_form.cleaned_data.get("title") content = article_form.cleaned_data.get("content") img = article_form.cleaned_data.get("content") article_obj = models.Article.objects.create(title=title, desc=content,create_time=datetime.datetime.now(),img=img,user_id=1) if article_obj: return HttpResponse('okokokokokok') else: pass article_form=ArticleForm() return render(request, 'addarticle.html',{'article_form':article_form})
# 添加文章图片预览 def uploadfile(request): file_obj = request.FILES.get('imgFile') file_name = file_obj.name from LoginEdit import settings import os path=os.path.join(settings.BASE_DIR,'app01','media',file_name) with open(path,'wb') as f: for i in file_obj: f.write(i) # 根据定值的大小上传 # for i in file_obj.chunks(): # f.write(i) response={ # 标识成功与否的状态,上传 'error':0, # 预览,src的路径就是下面的url 'url':'/media/'+file_name+'/' } import json return HttpResponse(json.dumps(response))
html
<script> KindEditor.ready(function (K) { window.editor = K.create('#id_content', { width: '600px', height: '500px', items: [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage', 'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ], {# 不能拖动0左右都不能拖拉,1左右不能,2都能#} resizeType: 2, {# 上传文件,文件的url地址#} uploadJson: '/uploadfile/', {# 因为传的是数据需要加入cstftoken#} extraFileUploadParams: { csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val() } }); }); </script>
详情见:
滑动验证
url(r'^login/', views.pcajax_validate), # 滑动 url(r'^pcgeetest/register',views.pcgetcaptcha,name='pcgetcaptcha'), url(r'^pcgeetest/ajax_validate',views.pcajax_validate,name='pcajaxvalidate'),
# huadong from app01.geetest import GeetestLib import json pc_geetest_id = "b46d1900d0a894591916ea94ea91bd2c" pc_geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4" mobile_geetest_id = "7c25da6fe21944cfe507d2f9876775a9" mobile_geetest_key = "f5883f4ee3bd4fa8caec67941de1b903" def pcgetcaptcha(request): user_id = 'test' gt = GeetestLib(pc_geetest_id, pc_geetest_key) status = gt.pre_process(user_id) request.session[gt.GT_STATUS_SESSION_KEY] = status request.session["user_id"] = user_id response_str = gt.get_response_str() return HttpResponse(response_str) def pcajax_validate(request): # 登陆也是这里,但是名字无法改 if request.method == "POST": loginResponse = {'is_login':False,'error_msg':None} # 验证码 gt = GeetestLib(pc_geetest_id, pc_geetest_key) challenge = request.POST.get(gt.FN_CHALLENGE, '') validate = request.POST.get(gt.FN_VALIDATE, '') seccode = request.POST.get(gt.FN_SECCODE, '') status = request.session[gt.GT_STATUS_SESSION_KEY] user_id = request.session["user_id"] if status: result = gt.success_validate(challenge, validate, seccode, user_id) else: result = gt.failback_validate(challenge, validate, seccode) if result: user = request.POST.get('username') pwd = request.POST.get('password') valid_code = request.POST.get('valid_code') valid_code_str = request.session.get('valid_code_str') if valid_code_str.upper() == valid_code.upper(): # 不区分大小写 # from django.contrib import auth print(user, pwd) # userobj = auth.authenticate(username=user,password=pwd) userobj = models.UserInfo.objects.filter(username=user, password=pwd).first() # 通过auth模块验证 对象 if userobj: # auth.login(request,userobj) # 设置SESSIO,保存的是一条记录对象 request.session['user'] = userobj.username loginResponse['user'] = userobj.username print('======_____++++++++', userobj.username) else: loginResponse['error_msg'] = 'username or password error' else: loginResponse['error_msg'] = 'valid code error' else: loginResponse['error_msg'] = 'yanzhengma error' return HttpResponse(json.dumps(loginResponse)) return render(request, "login.html")
<!-- 引入封装了failback的接口--initGeetest --> <script src="http://static.geetest.com/static/tools/gt.js"></script>
{# 方式一,刷新验证码,通过/get_valid_img/后面加入?刷新,不需要试图函数更改#} $("#valid_img").click(function () { $(this)[0].src += "?" }); # 滑动 var handlerPopup = function (captchaObj) { // 成功的回调 captchaObj.onSuccess(function () { var validate = captchaObj.getValidate(); $.ajax({ url: "/pc-geetest/ajax_validate", // 进行二次验证 type: "post", dataType: "json", data: { username: $('#user').val(), password: $('#pwd').val(), csrfmiddlewaretoken:$("[name='csrfmiddlewaretoken']").val(), valid_code: $('#valid_code').val(), geetest_challenge: validate.geetest_challenge, geetest_validate: validate.geetest_validate, geetest_seccode: validate.geetest_seccode }, success: function (data) { console.log('sss'); if (data.user) { console.log('dddddd'); if ($.cookie('next_path')) { location.href = $.cookie('next_path') } else { location.href = '/' } } else { $(".error").html(data.error_msg).css("color", "red"); {# setTimeout(function () {#} {# $(".error").text("")#} {# },1000)#} } } }); }); $("#loginbtn").click(function () { captchaObj.show(); console.log('11111') }); // 将验证码加到id为captcha的元素里 captchaObj.appendTo("#popup-captcha"); // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html }; // 验证开始需要向网站主后台获取id,challenge,success(是否启用failback) $.ajax({ url: "/pc-geetest/register?t=" + (new Date()).getTime(), // 加随机数防止缓存 type: "get", dataType: "json", success: function (data) { // 使用initGeetest接口 // 参数1:配置参数 // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件 initGeetest({ gt: data.gt, challenge: data.challenge, product: "popup", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效 offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注 // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config }, handlerPopup); } }); </script>