您的位置:首页 > 编程语言 > Go语言

django 富文本 登陆验证及跳转 及POST

2017-07-22 12:42 120 查看
1.

CKEDITOR

templates模板中

filter过滤器safe可讲html富文本进行渲染

<p>{{ one_article.content|safe }}</p>


2.

添加富文本编辑器

<head>
<script src="/static/plugins/ckeditor/ckeditor.js"></script>
</head>

<textarea id="id_content"></textarea>

<body>
<script>
CKEDITOR.replace("id_content");
</script>
</body>


3.

验证登录、及登录跳转

settings中设置登录跳转路径

settings.py

LOGIN_URL = '/signin/'


views.py

from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout

@login_required
#@login_required(login_url='/signin')也可以这样跳转

def new_article(request):
if request.method == "GET":


4.

POST GET对应方法。

@login_required
def new_article(request):
if request.method == "GET":
article_form = form.ArticleModelForm()
return render(request, 'bbs/new_article.html', {
'category_list':category_list,
'article_form':article_form,
})

elif request.method == "POST":
print(request.POST)
article_form = form.ArticleModelForm(request.POST, request.FILES)
if article_form.is_valid():
data = article_form.cleaned_data
#cleaned_data类似对象,无法直接编辑。
data['author_id'] = request.user.userprofile.id
article_obj = models.Article(**data)
article_obj.save()
return HttpResponse('new article saved!')

else:
return render(request, 'bbs/new_article.html', {
'category_list': category_list,
})


5.

post传输文件注意事项

enctype=”multipart/form-data”

<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{ article_form }}
<input type="submit" class="btn btn-success pull-right" style="margin-top: 20px" value="发布">
</form>


接收部分

article_form = form.ArticleModelForm(request.POST, request.FILES)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django