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

来源:互联网 发布:tengine windows版本 编辑:程序博客网 时间:2024/06/05 19:38

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_requiredfrom 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_requireddef 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)
原创粉丝点击