django admin後臺接入tinymce而且支持圖片上傳

首先:下載tinymcepython

地址是https://www.tinymce.com/jquery

點擊download數據庫

下載社區版本便可cookie

接着:把壓縮包內tinymce目錄內的全部文件和文件夾複製到Django項目中static/js目錄內:app

而後編輯admin文件編輯器

class BlogAdmin(admin.ModelAdmin):
    list_display = ("title", "author", "category", "public_time", "votes")
    search_fields = ("title", "author", "category")
    list_filter = ("public_time", "votes")
    ordering = ("-public_time",)

    class Media:
        js = (
            '/static/js/jquery-3.2.1.min.js',
            '/static/js/tinymce/js/tinymce/tinymce.min.js',
            '/static/js/tinymce/js/tinymce/jquery.tinymce.min.js',
            '/static/js/tinymce/js/tinymce/textareas.js'
        )
admin.site.register(Blog, BlogAdmin)
 

緊接着在/static/js/tinymce/js/tinymce目錄下新建一個textareas.js文件函數

tinymce.init({
    selector: "textarea",
    theme: "modern",
    plugins: ["image"],
    image_advtab: true,
    paste_data_images: true,
    file_browser_callback: function(field_name, url, type, win){
        if(type=='image'){
            $('#my_form input').click();
        };
    },
})

$( document ).ready(function() {
h ='<iframe id="form_target" name="form_target" style="display:none"></iframe><form id="my_form" action="/uploadImg/" target="form_target" method="post" enctype="multipart/form-data" style="width:0px;height:0;overflow:hidden"><input name="img" type="file" onchange="$(\'#my_form\').submit();this.value=\'\';"></form>';
$('body').append(h);
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
$('#my_form').append('<input type="hidden" name="csrfmiddlewaretoken" value='+csrftoken+' />');
});

此時已經集成了tinymce編輯器了,可是文件上傳功能還沒結束post

如今要建立一個model用來存放文件,一個url和視圖函數來處理圖片上傳,以及在setting中配置this

先一個個來,首先配置setting中的MEDIA_ROOTurl

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

接着編輯models文件

class BlogPic(models.Model):
    filename = models.CharField(max_length=200, blank=True, null=True)
    img = models.ImageField(upload_to='./media')

python manage.py makemigrations

python manage.py migrate

創建對應的數據庫

而後就是urls文件,添加上傳文件url

from blog.views import uploadImg

urlpatterns = [
    url(r'uploadImg', uploadImg, name='uploadImg'),
    url(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),
]

最後編輯views視圖函數

def uploadImg(request):
    img = request.FILES.get('img')
    adminImg = BlogPic()
    adminImg.filename = img.name
    adminImg.img = img
    adminImg.save()
    return HttpResponse("<script>top.$('.mce-btn.mce-open').parent().find('.mce-textbox').val('/media/%s')"
                        ".closest('.mce-window').find('.mce-primary').click();</script>" %adminImg.img)

完成這些步驟以後就已經集成了tinymce,而且支持圖片上傳了

參考文獻

http://blog.csdn.net/hjxzt1/article/details/78073596

http://blog.csdn.net/hjxzt1/article/details/78068972

相關文章
相關標籤/搜索