django是非常豐富功能強大的python web框架槽袄,而如果只是個人或小團隊爷辱,實驗的使用鳄乏,一般是原生admin后臺管理唠亚,xadmin是一款開源的更為強大豐富的后臺管理系統(tǒng)蚪战,可以方便我們對相關(guān)數(shù)據(jù)庫model和user的管理牵现。可以方便搭建一個企業(yè)級的后臺管理系統(tǒng)邀桑,很多團隊和企業(yè)也會基于其進行二次開發(fā)瞎疼,搭建個性化的系統(tǒng)。
github地址:https://github.com/sshwsfc/xadmin
網(wǎng)站說明:http://sshwsfc.github.io/xadmin/
特點
xadmin官網(wǎng)都有直觀詳細的介紹
主要是具有比原生admin更好的界面和體驗壁畸,基于Twitter Boostrap贼急,也有專門的個性化主題
http://x.xuebingsi.com
很多人也分享新的主題
另外可以直接簡便替換原生admin,只要更改setting文件和urls文件即可
豐富的插件化擴展功能支持捏萍,在包里直接增加即可太抓,如過濾器插件,數(shù)據(jù)提取插件
接下里就是數(shù)據(jù)相關(guān)的豐富功能令杈,可以支持日期走敌,數(shù)字的數(shù)據(jù)過濾等;全面的增刪改查功能逗噩,
支持xls 掉丽、json 等各種數(shù)據(jù)導(dǎo)出功能,
豐富的數(shù)據(jù)分析可視化功能等等异雁。
比如這是個模板
再看下requirement文件可以看到需要一些依賴
前面的兩個是必須的捶障,后門的可選,
xlwt纲刀,xlsxwriter 是支持Excel表格導(dǎo)出的包残邀,可以選擇。
廢話不說了,開始體驗吧
下載安裝xadmin
和其他python包類似可以pip下載 : pip install xadmin
推薦使用下載源碼安裝
pip install https://github.com/sshwsfc/xadmin/tarball/master
安裝成功后芥挣,將下載的xadmin包復(fù)制到項目目錄下:
項目主配置目錄refs下 setting文件加上三個應(yīng)用
增加路由
refs/urls.py增加
from django.conf.urls import url,include
from xadmin.plugins import xversion
import xadmin
xversion.register_models()
xadmin.autodiscover()
urlpatterns = [
#path("admin/", admin.site.urls),
path("xadmin"),xadmin.site.urls),
根據(jù)model的更改生成migrations文件驱闷,也就是數(shù)據(jù)遷移的動作文件
python manage.py makemigrations
問題1:
File "E:\workspace\SmartScientificResearchAssistant\xadmin\models.py", line 8, in <module>
from django.core.urlresolvers import NoReverseMatch, reverse
ModuleNotFoundError: No module named 'django.core.urlresolvers'
這里要使用django.urls,django.core.urlresolvers包其實沒有了空免,在xadmin里很多地方有這個地方
對model.py 文件里
from django.core.urlresolvers import NoReverseMatch, reverse
替換成
from django.urls import NoReverseMatch, reverse
還有很多其他文件也用到了 這個包空另,所以可以批量搜索替換含有 django.core.urlresolvers 的地方。
問題2
File "E:\workspace\SmartScientificResearchAssistant\xadmin\models.py", line 11, in <module>
from django.utils.encoding import python_2_unicode_compatible, smart_text
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (D:\ProgramData\Anaconda3\envs\myproject\lib\site-packages\django\utils\encoding.py)
使用
from six import python_2_unicode_compatible
替代
from django.utils.encoding import python_2_unicode_compatible, smart_text
問題3:
File "E:\workspace\SmartScientificResearchAssistant\xadmin\models.py", line 19, in <module>
from xadmin.util import quote
File "E:\workspace\SmartScientificResearchAssistant\xadmin\util.py", line 24, in <module>
from django.contrib.staticfiles.templatetags.staticfiles import static
ModuleNotFoundError: No module named 'django.contrib.staticfiles.templatetags'
將from django.contrib.staticfiles.templatetags.staticfiles import static
替換成
from django.templatetags.static import static
問題4:
File "E:\workspace\SmartScientificResearchAssistant\xadmin\models.py", line 46, in <module>
class Bookmark(models.Model):
File "E:\workspace\SmartScientificResearchAssistant\xadmin\models.py", line 48, in Bookmark
user = models.ForeignKey(AUTH_USER_MODEL, verbose_name=_(u"user"), blank=True, null=True)
TypeError: init() missing 1 required positional argument: 'on_delete'
這是model的數(shù)據(jù)定義不符合數(shù)據(jù)庫級聯(lián)方式蹋砚,外鍵記錄要隨著主鍵表的記錄刪除而刪除扼菠。
凡是出現(xiàn)關(guān)聯(lián)關(guān)系字段的 地方要加上,也就是ForeignKey地方加上on_delete=models.CASCADE
問題5:
model = ModelChoiceField(label=_(u'Target Model'), widget=exwidgets.AdminSelectWidget)
File "E:\workspace\SmartScientificResearchAssistant\xadmin\views\dashboard.py", line 284, in init
forms.Field.init(self, required, widget, label, initial, help_text,
TypeError: init() takes 1 positional argument but 6 were given
看到這里因為新的forms表單初始化只有一個參數(shù)坝咐,這里改為一個即可
問題6:
File "E:\workspace\SmartScientificResearchAssistant\xadmin\views\website.py", line 5, in <module>
from django.contrib.auth.views import login
ImportError: cannot import name 'login' from 'django.contrib.auth.views' (D:\ProgramData\Anaconda3\envs\myproject\lib\site-packages\django\contrib\auth\views.py)
將website.py中的
from django.contrib.auth.views import login
from django.contrib.auth.views import logout
替換為
from django.contrib.auth import authenticate,login,logout
問題7:
File "E:\workspace\SmartScientificResearchAssistant\xadmin\plugins\filters.py", line 10, in <module>
from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS
ImportError: cannot import name 'QUERY_TERMS' from 'django.db.models.sql.query' (D:\ProgramData\Anaconda3\envs\myproject\lib\site-packages\django\db\models\sql\query.py)
將plugins 里的filters
中from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS
替換為
from django.db.models.sql.query import LOOKUP_SEP,Query
問題8:
File "E:\workspace\SmartScientificResearchAssistant\xadmin\plugins\passwords.py", line 4, in <module>
from django.contrib.auth.views import password_reset_confirm
ImportError: cannot import name 'password_reset_confirm' from 'django.contrib.auth.views' (D:\ProgramData\Anaconda3\envs\myproject\lib\site-packages\django\contrib\auth\views.py)
xadmin\plugins\password.py 中的
from django.contrib.auth.views import password_reset_confirm
替換成
from django.contrib.auth.views import PasswordResetConfirmView
問題9:
File "D:\ProgramData\Anaconda3\envs\myproject\lib\site-packages\django\conf_init_.py", line 77, in getattr
val = getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'MIDDLEWARE_CLASSES
將xadmin\plugins\language.py 中的
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_CLASSES:
替換成
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE:
修改后循榆,進行數(shù)據(jù)庫遷移,python make migrate
也就是實際開始操作數(shù)據(jù)庫和模型一致墨坚。
啟動服務(wù)
python manage.py runserver 0.0.0.0:9090
登錄問題:
修改views\website.py文件
問題11
解決方案:
將xadmin\util.py 中的86行 def vendor(*tags):方法體改為:
css = {'screen': []}
js = []
for tag in tags:
file_type = tag.split('.')[-1]
files = xstatic(tag)
if file_type == 'js':
js.extend(files)
elif file_type == 'css':
css['screen'] += files
return Media(css=css, js=js)
可以登錄了
先要創(chuàng)建是超級管理員 :python .\manage.py createsuperuser
左邊已經(jīng)有基礎(chǔ)結(jié)構(gòu)模型秧饮,接著就可增加自己設(shè)計的model了,然后可以進行界面管理
對使用的django包的forms下的boundfield.py進行注釋
問題
對類似的field.rel 改為field.remote_field
有幾個地方泽篮,可以批量替換
'ManyToManyField' object has no attribute 'rel'
同樣改成remote_field
改為model
同樣改成
remote_field
國際化問題
File "E:\workspace\SmartScientificResearchAssistant\xadmin\sites.py", line 349, in i18n_javascript
from django.views.i18n import javascript_catalog
ImportError: cannot import name 'javascript_catalog' from 'django.views.i18n' (D:\ProgramData\Anaconda3\envs\myproject\lib\site-packages\django\views\i18n.py)
新的django中 javascript_catalog 方法不存在導(dǎo)致盗尸,頁面js代碼執(zhí)行錯誤
xadmin里的sites.py可以改為
并且在setting里加上應(yīng)用
這下各個部分都可以了。