安裝和配置xadmin
1. 安裝(注意區分版本)
- python2.x
pip install xadmin
- python3.x
# 方式1 pip install git+git://github.com/sshwsfc/xadmin.git # 方式2 下載https://codeload.github.com/sshwsfc/xadmin/zip/master,解壓並進入目錄下,直接python setup.py install
2. 配置
1. 將xadmin添加到settings.py應用列表javascript
INSTALLED_APPS = [ ..... # 須要添加如下兩個app 'xadmin', 'crispy_forms', ]
2. 修改項目中urls.pyjava
import xadmin urlpatterns = [ # url(r'^admin/', admin.site.urls), url(r'^xadmin/', xadmin.site.urls), ]
3. 在models.py中增長模型類node
from django.db import models class Article(models.Model): """ 文章表""" title = models.CharField(max_length=64, verbose_name='標題') author = models.CharField(max_length=32,verbose_name='做者', default='halo') content = models.TextField(verbose_name='內容') create_time = models.DateField(auto_now_add=True, editable=True,verbose_name='建立時間') def __str__(self): return self.title class Meta: verbose_name_plural = '文章表'
4. 在應用下新建adminx.py,用來註冊模型python
import xadmin from .models import * class ArticleAdmin(object): list_display = ('title', 'author', 'create_time') xadmin.site.register(Article, ArticleAdmin)
5. 數據遷移git
python manage.py makemigrations python manage.py migrate
6. 建立用戶github
python manager.py createsuperuser
7.啓動服務並訪問django
python manage.py runserver 8000
輸入用戶名和密碼登陸成功後以下所示:python3.x
DjangoUeditor安裝和配置
1.安裝
# python2.x 下載https://codeload.github.com/zhangfisher/DjangoUeditor/zip/master下的源碼包,用命令python setup.py install # python3.x 下載https://codeload.github.com/twz915/DjangoUeditor3/zip/master下的源碼包,用命令python setup.py install
2.配置
1. 將DjangoUeditor添加到settings.py應用列表app
INSTALLED_APPS = ( #........ 'DjangoUeditor', )
2.setting.py的其餘配置ssh
UEDITOR_SETTINGS={ "config":{ "toolbars":[['source','undo','redo','bold','italic','underline','forecolor','backcolor', 'superscript','subscript','justifyleft','justifycenter','justifyright','insertorderedlist', 'insertunorderedlist','blockquote','formatmatch','removeformat','autotypeset','inserttable', 'pasteplain','wordimage','searchreplace','map','preview','fullscreen'], ['insertcode','paragraph','fontfamily','fontsize','link','unlink','simpleupload','insertvideo', 'attachment','emotion','date','time']] }, }
3.配置url
from django.conf.urls.static import static from django.conf import settings url(r'^ueditor/', include('DjangoUeditor.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
4.配置adminx.py
import xadmin from .models import Article class ArticleAdmin(object): list_display = ('title', 'author', 'tg', 'create_time') style_fields = {"content": "ueditor"} # 新增長 xadmin.site.register(Article, ArticleAdmin)
5.配置xadmin:在xadmin/plugins下新建ueditor.py
import xadmin from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView from DjangoUeditor.models import UEditorField from DjangoUeditor.widgets import UEditorWidget from django.conf import settings class XadminUEditorWidget(UEditorWidget): def __init__(self,**kwargs): self.ueditor_options=kwargs self.Media.js = None super(XadminUEditorWidget,self).__init__(kwargs) class UeditorPlugin(BaseAdminPlugin): def get_field_style(self, attrs, db_field, style, **kwargs): if style == 'ueditor': if isinstance(db_field, UEditorField): widget = db_field.formfield().widget param = {} param.update(widget.ueditor_settings) param.update(widget.attrs) return {'widget': XadminUEditorWidget(**param)} return attrs def block_extrahead(self, context, nodes): js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js") # 本身的靜態目錄 js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.js") # 本身的靜態目錄 nodes.append(js) xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView) xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)
6.在xadmin/plugins/__init__.py添加ueditor
'ueditor'
7.在models下添加ueditor項
from DjangoUeditor.models import UEditorField class Article(models.Model): """ 文章表""" title = models.CharField(max_length=64, verbose_name='標題') author = models.CharField(max_length=32,verbose_name='做者', default='halo') # content = models.TextField(verbose_name='內容') content = UEditorField(verbose_name='內容', height=500, width=800, default=u'', imagePath="ueditor/image/", toolbars='full', filePath='ueditor/files/', upload_settings={"imageMaxSize": 1204000}, settings={}, command=None, ) create_time = models.DateField(auto_now_add=True, editable=True,verbose_name='建立時間') def __str__(self): return self.title class Meta: verbose_name_plural = '文章表'
8.頁面中顯示富文本
{% autoescape off %} {{ item.content }} {% endautoescape %}