url配置
url(r'^(?P<username>.*)/backindex/$',views.backIndex),
url(r'^(?P<username>.*)/backindex/addarticle/$',views.addarticle),
url(r'^(?P<username>.*)/backindex/delarticle/$',views.delarticle),
url(r'^(?P<username>.*)/backindex/editarticle/$',views.editarticle),
视图相关
添加文章
import datetime
def addarticle(request,username):
if request.method=="GET":
print("=============这里是直接通过后台跳转过来的")
article_form = ArticleForm()
cate_list = models.Category.objects.filter(blog__user=request.user)
tag_list = models.Tag.objects.filter(blog__user=request.user)
return render(request,"addarticle.html",locals())
else:
article_form = ArticleForm(request.POST)
if article_form.is_valid(): #验证
title = request.POST.get("title")
content = request.POST.get("content")
article_obj = models.Article.objects.create(title=title, desc=content[0:30],create_time=datetime.datetime.now(),user=request.user)
models.ArticleDetail.objects.create(content=content,article=article_obj)
return render(request,"addarticle.html",locals())
return render(request,"addarticle.html",locals())
编辑文章
def editarticle(request,username):
if request.method=="POST":
return HttpResponse("OK")
删除文章
def delarticle(request,username):
if request.method=="POST":
response = {"is_del":False}
article_nid = request.POST.get("article_nid")
models.Article.objects.filter(nid=article_nid).delete()
response["is_del"]=True
return HttpResponse(json.dumps(response))
前端页面
增加文章
{% extends "backendindex.html" %}
{% block manageCon %}
<span class="success" style="color: red"></span>
<form action="/blog/{{ request.user.username }}/backindex/addarticle/" method="post" novalidate>
{% csrf_token %}
<label for="title">文章标题</label>
<p>{{ article_form.title }}</p>
<label for="title">文章内容</label>
<p>{{ article_form.content }}</p>
<p>文章分类</p>
{% for cate in cate_list %}
<input type="radio" name="personal_cate" value="{{ cate.nid }}">{{ cate.title }}
{% endfor %}
<p>文章标签</p>
{% for tag in tag_list %}
<input type="checkbox" name="personal_tag" value="{{ tag.nid }}">{{ tag.title }}
{% endfor %}
</form>
<button id="submit">提交</button>
{% endblock %}
{% block script %}
<script>
function foo() {
$(".success").html("")
}
KindEditor.ready(function(K) {
window.editor = K.create('#editor_id',{
"800px",
height:"400px",
resizeType:0,
uploadJson:"/uploadFile/",
extraFileUploadParams:{
csrfmiddlewaretoken:$("[name='csrfmiddlewaretoken']").val(),
},
});
});
$("#submit").click(function () {
$.ajax({
url:"/blog/{{ request.user.username }}/backindex/addarticle/",
type:"POST",
data:{
title:$("[name=title]").val(),
content:editor.html(),
tag:$("[name=personal_tag]").val(),
cate:$("[name=personal_cate]").val(),
csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
},
success:function (data) {
editor.html(""),
$("[name=title]").val(""),
$(".success").html("添加成功"),
setTimeout(foo,3000)
}
})
});
</script>
{% endblock %}