Python3集成安装xadmin
Python3集成安装xadmin
1:创建虚拟环境
C:\Users\Adminstrator>mkvirtualenv -p C:\Python34\python.exe MyDjango
如果提示找不到,可以直接mkvirtualenv MyDjango
创建成功以后则终端会这样显示:
(MyDjango)C:\Users\Adminstrator
2:安装依赖,如果不安装依赖的话在使用xadmin的时候会各种报错。依赖列表如下:
django Pillow django-crispy-forms~=1.6.0 django-import-export>=0.5.1 django-reversion~=2.0.0 django-formtools future==0.15.2 httplib2==0.9.2 six==1.10.0 django-pure-pagination==0.3.0 django-simple-captcha mysqlclient requests xlrd XlsxWriter xlwt
使用pip install -i https://pypi.douban.com/simple django Pillow ..进行安装
使用豆瓣的源会比国外的下载更快
在安装mysqlclient可能会出现下面的错误(主要是windows环境下会有此问题):
error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows S
DK 7.1": www.microsoft.com/download/details.aspx?id=8279
----------------------------------------
Failed building wheel for mysqlclient
解决办法:去这个路径下下载mysqlclient:
https://www.lfd.uci.edu/~gohlke/pythonlibs/
根据每个人操作系统的情况下载对应的版本,然后放到安装路径下并进行安装。例如:
(MyDjango) C:\Users\CCD>pip install mysqlclient-1.3.12-cp34-cp34m-win32.whl
Processing c:\users\ccd\mysqlclient-1.3.12-cp34-cp34m-win32.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.12
安装完成以后,就可以在cmd下使用pip install查看是否已经安装完成。
3:新建一个Django工程,工程名取为DjangoLearn
我们通过源码的方式安装xadmin,下载xadmin源码,然后在工程中新建一个文件夹extra_apps
直接把xadmin源码文件夹拖到extra_apps中,因为新建的工程默认使用admin,所以我们需要进行一些修改
在工程中的settings.py文件夹中的INSTALLED_APPS增加 xadmin和crispy_forms,crispy-forms可以用来
渲染我们的表单。
然后修改我们的url,需要右击extra_apps将这个路径设置为Source Root:Mark Directory as Source Root
import xadmin urlpatterns = [ path('xadmin/', xadmin.site.urls), ]
如果Django工程没有配置数据库的话,就需要进行数据库的配置,数据库配置如下:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': "testdjango", 'USER': "root", 'PASSWORD': "123456", 'HOST': "127.0.0.1", } }
然后点击工程中的Tools一栏
选择Run manage.py Task
输入命令makemigrations和migrate进行数据库创建:
如果出现这样的错误:
Failed to get real commands on module "DjangoLearn": python process died with code 1: Traceback (most recent call last): File "D:\Program Files\JetBrains\PyCharm 2017.2.3\helpers\pycharm\_jb_manage_tasks_provider.py", line 25, in <module> django.setup() File "C:\Users\CCD\Envs\MyDjango\lib\site-packages\django\__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "C:\Users\CCD\Envs\MyDjango\lib\site-packages\django\apps\registry.py", line 85, in populate app_config = AppConfig.create(entry) File "C:\Users\CCD\Envs\MyDjango\lib\site-packages\django\apps\config.py", line 90, in create module = import_module(entry) File "C:\Users\CCD\Envs\MyDjango\lib\importlib\__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 2254, in _gcd_import File "<frozen importlib._bootstrap>", line 2237, in _find_and_load File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked ImportError: No module named 'xadmin'
则需要在setting中增加这样以下代码:
import sys
sys.path.insert(0,os.path.join(BASE_DIR,'extra_apps')) 这样就可以把extra_apps路径放到当前模块扫描的路径中
想登陆xadmin,需要先在Pycharm中创建用户
createsuperuser manage.py@DjangoLearn > createsuperuser "D:\Program Files\JetBrains\PyCharm 2017.2.3\bin\runnerw.exe" C:\Users\CCD\Envs\MyDjango\Scripts\python.exe "D:\Program Files\JetBrains\PyCharm 2017.2.3\helpers\pycharm\django_manage.py" createsuperuser F:/PycharmProjects/DjangoLearn Username (leave blank to use 'ccd'): CCD Email address: 123456789@qq.com Warning: Password input may be echoed. Password: ABC123456 Warning: Password input may be echoed. Password (again): ABC123456 Superuser created successfully. Following files were affected Process finished with exit code 0
然后访问http://127.0.0.1:8000/xadmin/
就可以登录上后台管理系统了