踩了很多坑,最后還是踩過了,很無奈??!先上最后的效果圖

圖片上傳

圖片顯示,文本編輯
1.DjangoUeditor-安裝
我使用的是python3,首先用pip安裝DjangoUeditor,
pip3 install DjangoUeditor //然后,坑開始了,歡迎補(bǔ)充坑
- 下載DjangoUeditor3
首先安裝后的DjangoUeditor版本,代碼沒有更新,只是適配了python2,你用python3當(dāng)然要出現(xiàn)問題了?。。。。?!
請點(diǎn)擊,然后下載 https://github.com/twz915/DjangoUeditor3/
- 替換部分不適配python3的DjangoUeditor
找到DjangoUeditor的位置,替換(DjangoUeditor3里面的DjangoUeditor文件夾替換到本地的DjangoUeditor)【替換后還需要修改一個(gè)類的頭文件引入,看具體報(bào)錯(cuò)找位置,修改起來很簡單】
Import sys
sys.path //找到python3 的本地下載庫

我本地的位置
2.django使用DjangoUeditor-代碼
- Setting.py
INSTALLED_APPS = (
#........
'DjangoUeditor',
)
//上傳路徑(很重要)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
UEDITOR_SETTINGS={
"toolbars":{ #定義多個(gè)工具欄顯示的按鈕,允行定義多個(gè)
"name1":[[ 'source', '|','bold', 'italic', 'underline']],
"name2",[]
},
"images_upload":{
"allow_type":"jpg,png", #定義允許的上傳的圖片類型
"max_size":"2222kb" #定義允許上傳的圖片大小,0代表不限制
},
"files_upload":{
"allow_type":"zip,rar", #定義允許的上傳的文件類型
"max_size":"2222kb" #定義允許上傳的文件大小,0代表不限制
},,
"image_manager":{
"location":"" #圖片管理器的位置,如果沒有指定,默認(rèn)跟圖片路徑上傳一樣
},
}
- urls.py
url(r'^ueditor/',include('DjangoUeditor.urls' )),
# 如果不設(shè)置,上傳后將看不到圖片
# use Django server /media/ files
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
- models
from DjangoUeditor.models import UEditorField
class Blog(models.Model):
Name=models.CharField(max_length=100,blank=True)
Content=UEditorField('內(nèi)容 ',height=100,width=500,default='test',imagePath="uploadimg/",imageManagerPath="imglib",toolbars='mini',options={"elementPathEnabled":True},filePath='upload',blank=True)
- forms
from DjangoUeditor.forms import UEditorField
class TestUEditorForm(forms.Form):
Description=UEditorField("描述",initial="abc",width=600,height=800)
- views
from blog.forms.TestUeditorModelForm import TestUEditorForm
form = TestUEditorForm()
return render_to_response('topic/topicHome.html',{"form": form})
- html
先運(yùn)行 python3 manage.py collectstatic
<head>
......
{{ form.media }} #這一句會(huì)將所需要的CSS和JS加進(jìn)來。
......
</head>
<div class="edit-area">
{{ form }}
</div>
感概一下,python學(xué)習(xí)真的資料太少了,坑太多了,很多地方的代碼和邏輯都是我東平西湊補(bǔ)全的。另外我嚴(yán)重懷疑,很多寫博客的絕對自己項(xiàng)目都沒跑起來,然后就把我?guī)У搅松羁永?,難受?。。。?!。